“ detail”:“不允许使用方法\” GET \“。 Django Rest框架

时间:2019-08-01 05:52:59

标签: python django api django-rest-framework http-post

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

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

编辑:

仍然出现此错误,

err

3 个答案:

答案 0 :(得分:1)

您需要将GET端点URL添加到urls.py中才能使用GET请求。您的urls.py中缺少GET网址,只需像这样编辑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)

答案 1 :(得分:1)

基本上,问题在于,视图中没有为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视图。

仅供参考permission_classes无法与APIView一起使用。您需要使用GenericAPIView或任何其他常规视图才能具有这些功能。

答案 2 :(得分:1)

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

renderer_class = api_settings.DEFAULT_RENDERER_CLASSES

我更正了:

renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES 

它对我有用。