2
我正在開發一個使用Django rest框架的API。在我的本地,一切都很好。我也用swagger來記錄我的API,我可以看到我的帖子,並在swagger中獲取方法。Django rest框架在部署後返回405後的方法
問題是,當我將我的代碼部署到服務器時,我的郵政方法獲得了405個代碼,無法在swagger上看到發佈方法。他們只是消失。
這裏是我的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)
爲什麼我收到405的郵政方法?
405通常意味着你正在向錯誤的URL發送POST請求。 –
@KevinBrown我只有一個網址是「/ Clinic /」,並且由於某種原因返回405。 – Sefa