问题描述
我正在尝试使用Django Rest Framework JSON API创建符合JSON API的REST服务:
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
我认为我仍然停留在Django Rest Framework级别,但是我不确定。
I think I'm stuck at the Django Rest Framework level, but I am not certain.
我认为GET请求有效,但是POST并不是因为我收到此响应:
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"
}
]
}
但是GET很好:
$ curl -H 'Accept: application/vnd.api+json; indent=2' http://localhost:8000/greeters/
{
"data": {}
}
我看过其他文章和教程,似乎我错过了代码中没有出现的某些内容。希望它会在对Djang和Django Rest Framework有更多经验的人中弹出。
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.
谢谢您的时间:)
这是我的代码:
my_project / urls.py
my_project/urls.py
from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^greeters/', include('greeter.urls')),
]
greeter / urls.py
greeter/urls.py
from django.conf.urls import url, include
from rest_framework import routers
from .views import GreeterViewSet
ROUTER = routers.DefaultRouter()
ROUTER.register(r'^', GreeterViewSet)
urlpatterns = [
url(r'^', include(ROUTER.urls)),
]
greeter / views.py
greeter/views.py
from rest_framework import viewsets
from .models import Greeter
from .serializers import GreeterSerializer
class 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
greeter/serializers.py
from rest_framework import serializers
from .models import Greeter
class GreeterSerializer(serializers.HyperlinkedModelSerializer):
"""Define Greeter serializer"""
class Meta:
model = Greeter
fields = ('message')
问候/models.py
greeter/models.py
from django.db import models
class Greeter(models.Model):
"""Define Greeter model"""
id = models.CharField(
primary_key=True,
max_length=200
)
message = models.CharField(max_length=200)
更新:
谢谢您的帮助。
我删除了 greeter / urls.py
并移动了所有url配置进入 my_project / urls.py
。
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 GreeterViewSet
ROUTER = routers.DefaultRouter()
ROUTER.register(r'greeters', GreeterViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include(ROUTER.urls)),
]
它可以工作,但是我仍然想了解如何将所有欢迎URL代码推送到greeter文件夹内的文件中,例如 greeter / urls.py
。
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?
再次感谢所有帮助:)
推荐答案
添加一些 API前缀到您的网址包括语句,
Add some API prefix to your url include statement as,
urlpatterns = [
url(r'sample', include(ROUTER.urls)),
]
这样,您的端点将是 / greeters / sample /
用于list-api(HTTP GET)并创建api( HTTP POST)
Thus your end-point will be, /greeters/sample/
for list-api (HTTP GET) and create api (HTTP POST)
这篇关于方法“开机自检” Django Rest Framework不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!