“detail”:“Method \“GET\”not allowed.”Django Rest Framework

lnlaulya  于 2023-05-08  发布在  Go
关注(0)|答案(4)|浏览(213)

我知道这个问题可能是重复的,但我试过很多解决方案,都不能理解。我已经完全按照本教程,但我得到这个错误的'用户列表'页面。其他一切都很好。有人能指出错误是什么吗?

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)

编辑: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()),

编辑:
仍然得到这个错误,

zte4gxcn

zte4gxcn1#

您需要将GET端点url添加到urls.py以使用GET请求。GET url在你的urls.py中丢失,只需编辑你的urls.py如下:

# 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()),
              ]

你需要在你的UserList视图中实现get方法,比如:

# 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)
avkwfej4

avkwfej42#

基本上问题是,在您的视图中没有为GET请求定义功能。你可以添加它,像这样:

class UserList(APIView):
    permission_classes = (permissions.AllowAny,)
    http_method_names = ['get', 'head', 'post']

    def get(self, request, *args, **kwargs):
        serializer = UserSerializerWithToken(User.objects.all(), many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    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)

也可以从ListAPIView继承UserList View的子类。

仅供参考permission_classes无法与APIView一起使用。您需要使用GenericAPIView或任何其他通用视图来实现这些功能。

e5nqia27

e5nqia273#

我有同样的问题,但我注意到,在我的views.py,我有

renderer_class = api_settings.DEFAULT_RENDERER_CLASSES

我更正为:

renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES

对我很有效

ycl3bljg

ycl3bljg4#

也许你是从你打电话的地方打电话给“post”,例如“postman”
但你的功能是

def get(self, request, *args, **kwargs):

所以调用“get”或将函数更改为

def post(self, request, *args, **kwargs):

相关问题