【问题标题】:{ "detail": "Method \"GET\" not allowed." }{ "detail": "方法 \"GET\" 不允许。" }
【发布时间】:2019-06-27 10:08:15
【问题描述】:

所以我已经提到了这个问题。我是初学者,我可能会犯一些愚蠢的错误。如此谦虚地请求你们解决我的错误并帮助我完成我的项目。提前谢谢大家。

setting.py

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticatedOrReadOnly',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
}

JWT_AUTH = {
    'JWT_ALLOW_REFRESH': True,
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3600),
}

这是 setting.py 文件,这里我提到了所需的文件

view.py

class LoginViewSet(viewsets.ViewSet):
    """ Check email and password and return auth token. """

    serializer_class = AuthTokenSerializer
    authentication_classes((SessionAuthentication, TokenAuthentication, BasicAuthentication))
    permission_classes((IsAuthenticated,))

    def create(self, request):
        """ Use ObtainAuthToken APIView to validate and create a token. """

        return ObtainAuthToken().post(request)

这是 view.py 文件。

urls.py

router.register('login', views.LoginViewSet, base_name="login")

urlpatterns = [
    path('', include(router.urls)),
    path('login/', ObtainAuthToken.as_view()),
    path(r'api-token-auth/', obtain_jwt_token),
    path(r'api-token-refresh/', refresh_jwt_token),
]

错误信息 error

【问题讨论】:

  • 能否附上你的模板代码,我感觉你的表单可能有问题。
  • 看起来您正在向仅接受 POST(和 OPTIONS)请求的端点发送 GET 请求。对与身份验证相关的端点使用 POST 请求是有意义的,所以我认为这里的代码没有任何问题。更改请求方法可能会解决问题。
  • @Param 实际上我使用 Angular 6 作为后端,登录时我没有得到任何响应。甚至没有错误消息。当单击 bttn 时没有任何反应。就我而言,我没有后端方面的错误。
  • @PashupatiPariyar 您这里的代码看起来不错,因为 Fynn Becker 说您的身份验证表单正在向服务器发送 get 请求,而不是 post 请求,所以那里肯定有问题,Django 赢了'不干扰客户端'表单发送'。

标签: python django django-rest-framework


【解决方案1】:

您必须在 DRF 中定义 http_method_names 并在您的 api 中指定您的方法访问权限。

class IndexViewAPI(generics.GenericAPIView):
    http_method_names = ['get', 'head']
    # some statements

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 2019-12-09
    • 2020-06-17
    • 2021-09-23
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2016-01-31
    相关资源
    最近更新 更多