python runtime_error ai_generated true

werkzeug.exceptions.MethodNotAllowed: 405 方法不被允许:请求的 URL 不允许该请求方法。

werkzeug.exceptions.MethodNotAllowed: 405 Method Not Allowed: The method is not allowed for the requested URL.

ID: python/flask-405-method-not-allowed

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-04-09首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

路由注册的 HTTP 方法与客户端发送的方法不一致(例如只注册了 GET 而客户端发送 POST/PUT/DELETE)。

English

The route is registered for a different HTTP method (e.g. only GET) while the client sends POST/PUT/DELETE.

generic

解决方案

  1. 96% 成功率
    @app.route('/submit', methods=['GET', 'POST'])
    def submit(): ...
  2. 90% 成功率
    @app.post('/submit')
    def submit_post(): ...

无效尝试

常见但无效的做法:

  1. 85% 失败

    CSRF produces 400/403, not 405; the route method list is the actual cause.

  2. 80% 失败

    Masks the real routing mismatch and breaks clients.