gpt4 book ai didi

python - Django rest 框架在部署后为 post 方法返回 405

转载 作者:行者123 更新时间:2023-12-05 07:55:33 24 4
gpt4 key购买 nike

我正在使用 Django rest 框架开发 API。在我本地,一切都很好。我还使用 swagger 来记录我的 API,我可以在 swagger 上查看我的帖子和获取方法。

问题是,当我将代码部署到服务器时,我的 post 方法收到 405 代码,并且在 swagger 上看不到 post 方法。它们就这么消失了。

这是我的 API:

Views.py:

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from api.models import Clinic
from api.serializers import ClinicSerializer
from rest_framework.views import APIView
from django.http import Http404
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view



class ClinicList(APIView):
"""
List all clinics
"""
def get(self, request, format = None):
clinics = Clinic.objects.all()
serializer = ClinicSerializer(clinics, many = True)
return Response(serializer.data)
"""
Save new clinic
"""
def post(self, request, format = None):
serializer = ClinicSerializer(data=request.data)
if(serializer.is_valid()):
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

解决方案 urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('api.urls')),
url(r'^docs/', include('rest_framework_swagger.urls')),
]

api.urls:

    urlpatterns = [
url(r'^Clinic/$', views.ClinicList.as_view()),
]

urlpatterns = format_suffix_patterns(urlpatterns)

为什么我的 post 方法会收到 405?

最佳答案

服务器重启后问题就解决了。代码似乎没有问题。

关于python - Django rest 框架在部署后为 post 方法返回 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29682767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com