“详细信息”:“不允许使用“GET”方法。” Django Rest 框架

问题描述 投票:0回答:4

我知道这个问题可能是重复的,但我尝试了很多解决方案,但无法理解任何解决方案。我完全按照本教程进行操作,但在“用户列表”页面上出现此错误。其他一切都很好。有人可以指出错误是什么吗?

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)

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

编辑:

仍然出现此错误,

err

python django django-rest-framework http-post
4个回答
7
投票

您需要将 GET 端点 url 添加到您的

urls.py
才能使用 GET 请求。您的
urls.py
中缺少 GET url,只需编辑您的
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()),
              ]

并且您需要在

get
视图中实现
UserList
方法,例如:

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


2
投票

基本上问题是,在您的视图中没有为

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)

或者您可以从

UserList
 子类化 
ListAPIView
视图。

仅供参考

permission_classes
不能与
APIView
一起使用。您需要使用
GenericAPIView
或任何其他通用视图才能拥有这些功能。


2
投票

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

renderer_class = api_settings.DEFAULT_RENDERER_CLASSES

我将其更正为:

renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES 

它对我有用。


0
投票

也许您正在从任何地方调用“post”,例如“postman”

但是你的功能是

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

因此调用“get”或将您的函数更改为

def post(self, request, *args, **kwargs):
© www.soinside.com 2019 - 2024. All rights reserved.