pythondjangodjango-rest-frameworkhttp-post

"detail": "Method \"GET\" not allowed." Django Rest Framework


I know this question maybe a duplicate, but I have tried many solutions and could not understand any. I have followed this tutorial exactly and yet I get this error on the 'userlist' page. Everything else works just fine. Can somebody point out what the error is ?

class UserList(APIView):
"""
Create a new user. It's called 'UserList' because normally we'd have a get
method here too, for retrieving a list of all User objects.
"""

permission_classes = (permissions.AllowAny,)
http_method_names = ['get', 'head']

def post (self, request, format=None):
    self.http_method_names.append("GET")

    serializer = UserSerializerWithToken(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)

EDIT: urls.py

from django.urls import include, path
from classroom.views.classroom import current_user, UserList
from .views import classroom, suppliers, teachers
urlpatterns = [path('', classroom.home, name='home'),
               path('current_user/', current_user),
               path('users/', UserList.as_view()),

Edit:

Still getting this error,

err


Solution

  • You need to add GET endpoint url to your urls.py in order to use GET requests. GET url is missing in your urls.py, simply edit your urls.py like:

    # urls.py
    
    from django.urls import include, path
    from classroom.views.classroom import current_user, UserList
    from .views import classroom, suppliers, teachers
    
    urlpatterns = [
                   path('', classroom.home, name='home'),
                   path('current_user/', current_user),
                   path('users/', UserList.as_view()),
                   path('users/<int:pk>/', UserList.as_view()),
                  ]
    

    And you need to implement get method in yourUserList view such as:

    # views.py
    
    class UserList(APIView):
        """
        Create a new user. It's called 'UserList' because normally we'd have a get
        method here too, for retrieving a list of all User objects.
        """
    
        permission_classes = (permissions.AllowAny,)
        http_method_names = ['get', 'head']
    
    
        def get(self, request, format=None):
            users = User.objects.all()
            serializer = UserSerializerWithToken(users, many=True)
            return Response(serializer.data)
    
        def post(self, request, format=None):
            self.http_method_names.append("GET")
    
            serializer = UserSerializerWithToken(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)