相关疑难解决方法(0)

Django/DRF - 405 DELETE操作不允许使用方法

我正在使用本地计算机上的两台开发服务器(node&django).

我已经添加django-cors-headers到项目中以允许所有来源和方法(在dev上)具有以下设置:

CORS_ORIGIN_ALLOW_ALL = 'ALL'
CORS_ALLOW_METHODS = (
        'GET',
        'POST',
        'PUT',
        'PATCH',
        'DELETE',
        'OPTIONS'
    )
Run Code Online (Sandbox Code Playgroud)

尝试DELETE时我得到405.查看响应标头

HTTP/1.0 405 METHOD NOT ALLOWED
Date: Mon, 03 Nov 2014 10:04:43 GMT
Server: WSGIServer/0.1 Python/2.7.5
Vary: Cookie
X-Frame-Options: SAMEORIGIN
Content-Type: application/json
Access-Control-Allow-Origin: *
Allow: GET, POST, HEAD, OPTIONS
Run Code Online (Sandbox Code Playgroud)

请注意,DELETE&PATCH/ PUT不在允许的方法列表中.

我的配置中是否缺少某些内容?

django cors django-rest-framework django-cors-headers

14
推荐指数
2
解决办法
1万
查看次数

为什么 Django 没有 405 的错误页面处理程序 - 不允许的方法?

当我阅读 Django 文档时,我只发现了以下处理程序:400、403、404、500 错误。

为什么 405 错误处理程序不存在,但像 require_POST() 这样的装饰器很常见?

关键是,为 Method Not Allowed 错误创建自定义错误页面的正确方法是什么?


我使用 Django Middleware 解决了我的问题,也许这会对某人有所帮助

from django.http import HttpResponseNotAllowed
from django.shortcuts import render


class HttpResponseNotAllowedMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)

        if isinstance(response, HttpResponseNotAllowed):
            return render(request, '405.html', status=405)

        return response
Run Code Online (Sandbox Code Playgroud)

django

5
推荐指数
1
解决办法
861
查看次数