Home > Back-end >  I am gettitng post method not allowed
I am gettitng post method not allowed

Time:05-23

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)

I am getting post method not allowed, i don't know why i keep getting post method not allowed. The source code is below. Thanks

from django.urls import path
from .views import ListingView, SearchView, ListingsView, contact


urlpatterns = [
    path('contact/', contact)
]

CodePudding user response:

I think it's not about POST method. It's the authentication problem. You need to add the permission_classes decorator.

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):
    ...

Hope it could help.

  • Related