Django Rest Framework不允许使用方法“ POST”

时间:2018-09-06 19:42:42

标签: python django django-rest-framework json-api

我正在尝试使用Django Rest Framework JSON API创建兼容JSON API的Rest服务: docs

我认为我仍然停留在Django Rest Framework级别,但是我不确定。

我认为GET请求有效,但POST并不是因为我得到了以下响应:

$ 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有更多经验的其他人中弹出。

谢谢您的时间:)

这是我的代码:

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

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

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

from rest_framework import serializers
from .models import Greeter


class GreeterSerializer(serializers.HyperlinkedModelSerializer):
    """Define Greeter serializer"""

    class Meta:
        model = Greeter
        fields = ('message')

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,并将所有网址配置都移到了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

我打开了一个单独的问题以获取有关此问题的指导,以便将其标记为已回答。

https://django-rest-framework-json-api.readthedocs.io/en/stable/index.html

再次感谢您的所有帮助:)

1 个答案:

答案 0 :(得分:1)

在您的网址包含语句中添加一些 API前缀

urlpatterns = [
    url(r'sample', include(ROUTER.urls)),
]


因此,您的端点将是 /greeters/sample/ ,用于list-api(HTTP GET)和创建api(HTTP POST)