I'm trying to create a JSON API compliant rest service using Django Rest Framework JSON API: https://django-rest-framework-json-api.readthedocs.io/en/stable/index.html
I think I'm stuck at the Django Rest Framework level, but I am not certain.
I think the GET request is working, but the POST is not because I get this response:
$ curl -H 'Accept: application/vnd.api+json; indent=2' -X POST http://localhost:8000/greeters/
{"errors": [{"detail": "Method \"POST\" not allowed.","source": {"pointer": "/data"},"status": "405"}]
}
But the GET is fine:
$ curl -H 'Accept: application/vnd.api+json; indent=2' http://localhost:8000/greeters/
{"data": {}
}
I've looked at other posts and the tutorials and it seems like I've missed something in my code that is not popping out at me. Hopefully, it will pop out at someone else who has more experience with Djang and Django Rest Framework.
Thank you for your time :)
Here's my code:
my_project/urls.py
from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_pathurlpatterns = [path('admin/', admin.site.urls),re_path(r'^greeters/', include('greeter.urls')),
]
greeter/urls.py
from django.conf.urls import url, include
from rest_framework import routers
from .views import GreeterViewSetROUTER = routers.DefaultRouter()
ROUTER.register(r'^', GreeterViewSet)urlpatterns = [url(r'^', include(ROUTER.urls)),
]
greeter/views.py
from rest_framework import viewsets
from .models import Greeter
from .serializers import GreeterSerializerclass GreeterViewSet(viewsets.ModelViewSet):"""API endpoints for Greeter"""queryset = Greeter.objects.all()serializer_class = GreeterSerializer# I've tried adding these methods in but none of it worked#def perform_create(self, serializer):# serializer.save(owner=self.request.user)#def create(self, request):# print('#########create')#def post(self, request):# print('#########post')
greeter/serializers.py
from rest_framework import serializers
from .models import Greeterclass GreeterSerializer(serializers.HyperlinkedModelSerializer):"""Define Greeter serializer"""class Meta:model = Greeterfields = ('message')
greeter/models.py
from django.db import modelsclass Greeter(models.Model):"""Define Greeter model"""id = models.CharField(primary_key=True,max_length=200)message = models.CharField(max_length=200)
Update:
Thank you for the help. It has guided me to the solution.
I removed greeter/urls.py
and moved all of the url configurations into my_project/urls.py
.
from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_path
from rest_framework import routers
from greeter.views import GreeterViewSetROUTER = routers.DefaultRouter()
ROUTER.register(r'greeters', GreeterViewSet)urlpatterns = [path('admin/', admin.site.urls),re_path(r'^', include(ROUTER.urls)),
]
It works, but I would still like to understand how to push all of the greeter url code into a file inside the greeter folder, like greeter/urls.py
.
I've opened up a separate question to get guidance on that so I can mark this as answered.
In Django, how do you keep a module's url configurations encapsulated inside the module?
Thanks again for all of the help :)