简体   繁体   English

"detail": "方法 \"GET\" 不允许。" Django Rest 框架

[英]"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编辑: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,仍然收到此错误,

呃

You need to add GET endpoint url to your urls.py in order to use GET requests.您需要将 GET 端点 url 添加到您的urls.py以使用 GET 请求。 GET url is missing in your urls.py , simply edit your urls.py like:您的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()),
              ]

And you need to implement get method in your UserList view such as:并且您需要在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)

Basically the problem is that, there is not functionality defined for GET requests in your view.基本上问题在于,您的视图中没有为GET请求定义功能。 So either you can add it, like this:因此,您可以添加它,如下所示:

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)

Or you can subclass UserList View from ListAPIView .或者您可以从ListAPIView子类化UserList View 。

FYI , permission_classes won't work with APIView .仅供参考permission_classes不适用于APIView You need to use GenericAPIView or any other generic views to have those functionalities.您需要使用GenericAPIView或任何其他通用视图来拥有这些功能。

I had the same issue but I noticed that in my views.py, I had我有同样的问题,但我注意到在我的 views.py 中,我有

renderer_class = api_settings.DEFAULT_RENDERER_CLASSES

I corrected it to :我更正为:

renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES 

and it worked for me.它对我有用。

@api_view(['POST'])
def taskupdate(request,pk):
    serializershow=taskserializer(tasks)
   serializer=taskserializer(instance=tasks,data=request.data)
   if serializer.is_valid():
      serializer.save()
      Response(serializer.data,serializershow)

maybe you are calling 'post' from where ever you are calling, example as 'postman'也许你从你打电话的地方打电话给'post',例如'postman'

but your function is但你的 function 是

def get:

so call as 'get' or change your function to所以调用'get'或将您的 function 更改为

def post:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &quot;detail&quot;: &quot;方法 \\&quot;GET\\&quot; 不允许。&quot; 在 TokenAuthentication Django 休息框架中 - "detail": "Method \"GET\" not allowed." in TokenAuthentication Django rest framework “详细信息”:“方法 \”GET\“ 不允许。” Django Rest 框架 - “detail”: “Method \”GET\“ not allowed.” Django Rest Framework Django REST 框架 - “方法 \”GET\“ 不允许。” - - Django REST framework - “Method \”GET\“ not allowed.” - { &quot;detail&quot;: &quot;方法 \\&quot;GET\\&quot; 不允许。&quot; } - { "detail": "Method \"GET\" not allowed." } django“详细信息”:“方法\\”GET\\“不允许。” (一对多关系) - django “detail”: “Method \”GET\“ not allowed.” (one to many relationship) “详细信息”:“方法 \\”GET\\” 不允许。在 Django 中调用端点 - “detail”: “Method \”GET\" not allowed. on calling endpoint in django DRF- Django Rest 框架 re_path 查询参数 - “方法 \”GET\“ 不允许。” - DRF- Django Rest Framework re_path query params - “Method \”GET\“ not allowed.” DRF:“详细信息”:“方法\\” GET \\“不允许。” - DRF: “detail”: “Method \”GET\“ not allowed.” POST 返回“详细信息”:“不允许使用方法 \"GET\"。” - POST returns "detail": "Method \"GET\" not allowed." &quot;detail&quot;: &quot;方法 \\&quot;POST\\&quot; 不允许。&quot; - "detail": "Method \"POST\" not allowed."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM