我知道这个问题可能是重复的,但我尝试了很多解决方案,但无法理解任何一个。我完全按照本教程进行操作,但我在“用户列表”页面上收到此错误。其他一切都很好。有人可以指出错误是什么吗?
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()),
编辑:
仍然收到此错误,
原文由 Rohit Kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要将 GET 端点 url 添加到您的
urls.py
才能使用 GET 请求。 GET url 在你的urls.py
中丢失,只需编辑你的urls.py
就像:并且您需要在您的
UserList
视图中实施get
方法,例如: