我收到 post 不允许的方法
I am getting post method not allowed
from rest_framework.decorators import api_view
@api_view(['GET', 'POST'])
def contact(request):
if request.method == 'GET':
contact = ContactMe.objects.all()
serializer = ContactMeSerializer(contact, many=True)
return JsonResponse(serializer.data, safe=True)
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = ContactMeSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.data, safe=False)
我收到 post 不允许的方法,我不知道为什么我一直收到 post 不允许的方法。源代码如下。谢谢
from django.urls import path
from .views import ListingView, SearchView, ListingsView, contact
urlpatterns = [
path('contact/', contact)
]
我认为这与 POST
方法无关。这是身份验证问题。
您需要添加 permission_classes
装饰器。
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
@api_view(['GET', 'POST'])
@permission_classes([AllowAny])
def contact(request):
...
from rest_framework.decorators import api_view
@api_view(['GET', 'POST'])
def contact(request):
if request.method == 'GET':
contact = ContactMe.objects.all()
serializer = ContactMeSerializer(contact, many=True)
return JsonResponse(serializer.data, safe=True)
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = ContactMeSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.data, safe=False)
我收到 post 不允许的方法,我不知道为什么我一直收到 post 不允许的方法。源代码如下。谢谢
from django.urls import path
from .views import ListingView, SearchView, ListingsView, contact
urlpatterns = [
path('contact/', contact)
]
我认为这与 POST
方法无关。这是身份验证问题。
您需要添加 permission_classes
装饰器。
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
@api_view(['GET', 'POST'])
@permission_classes([AllowAny])
def contact(request):
...