【问题标题】:Django Unexpected 405 ResponseDjango 意外的 405 响应
【发布时间】:2019-05-08 19:24:14
【问题描述】:
  1. 问题

    我的 django 应用程序经常回复405 Method Now Allowed,即使 它是开发环境中的工作 api。但是如果我重新启动 开发服务器 (python manage.py runserver) 它可以工作。

  2. 环境

    • macOS Mojave 10.14
    • Python 3.6.4(通过 Pipenv 隔离环境)
    • Django 2.1.4
    • djangorestframework 3.8.2
  3. API 代码

    settings/urls.py(根 url 文件)

    from django.urls import path, include
    
    import my_account.urls
    
    urlpatterns = [
        path('account/', include(my_account.urls, namespace='account_v1')),
    ]
    

    我的帐户/urls.py

    from django.urls import path
    
    from .apps import MyAccountConfig
    from .views import TokenView
    
    app_name = MyAccountConfig.name
    
    urlpatterns = [
        path('token/', TokenView.as_view()),
    ]
    

    my_account/views.py

    from rest_framework.views import APIView
    
    class TokenView(APIView):
        def post(self, request):
            # Some Business-Logic Code
            pass
    
  4. 日志

    405 发生时的日志

    System check identified no issues (0 silenced).
    December 07, 2018 - 11:22:54
    Django version 2.1.4, using settings 'my_server.settings.staging'
    Starting development server at http://0.0.0.0:8000/
    Quit the server with CONTROL-C.
    .env Applied
    [07/Dec/2018 11:24:30] "OPTIONS /account/token/ HTTP/1.1" 200 0
    [07/Dec/2018 11:24:30] "{"email":"email@hidden.com","password":"hidden_password"}POST /account/token/ HTTP/1.1" 405 66
    

    工作时记录

    System check identified no issues (0 silenced).
    December 07, 2018 - 11:48:01
    Django version 2.1.4, using settings 'my_server.settings.staging'
    Starting development server at http://0.0.0.0:8000/
    Quit the server with CONTROL-C.
    .env Applied
    [07/Dec/2018 11:48:08] "OPTIONS /account/token/ HTTP/1.1" 200 0
    [07/Dec/2018 11:48:09] "POST /account/token/ HTTP/1.1" 200 517
    

    在 chrome 网络标签中,200 和 405 的请求没有区别。

【问题讨论】:

    标签: python django django-rest-framework http-status-code-405


    【解决方案1】:

    在过去的两个月里我也遇到了同样的错误,最终我解决了。 错误: 当用户令牌过期时,我们将收到带有未经授权消息的 401 状态代码,当这种情况发生时,下一个 API 也会被中断,它会返回 405 Method Not Allowed 作为错误。
    解决方案 : Django 2.1 版本存在错误问题,您必须将 django 版本升级到 2.2。

    检查此错误后它不会引发。

    【讨论】:

      【解决方案2】:

      HTTP_405_METHOD_NOT_ALLOWED

      状态码 405 表示您正在以不允许的方法请求。 根据您的代码,您将 Token API 定义为可通过 post 方法访问。

      看看这个日志:

      [07/Dec/2018 11:48:09] "POST /account/token/ HTTP/1.1" 200 517
      

      在此您发送明确的POST 请求,这没关系。但请查看提供的失败案例日志:

      [07/Dec/2018 11:24:30] "{"email":"email@hidden.com","password":"hidden_password"}POST /v1/account/token/ HTTP/1.1" 405 66
      

      1- 出于某种原因,在您的 UI 中,您正在使用方法 {"email":"email@hidden.com","password":"hidden_password"}POST 发送请求!!!!

      2- 您正在向/v1/account/token/ API 端点发送请求,该端点根本没有在您的网址中注册(基于您提供给我们的信息)

      【讨论】:

      • 抱歉,/v1/account/token/ 是我的拼写错误。并且,在 chrome 检查中,请求是有效的并且没有区别。 (另外,抓包工具也一样)
      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      相关资源
      最近更新 更多