我想做以下几件事:
raise HttpResponseForbidden()
但是我得到了一个错误:
exceptions must be old-style classes or derived from BaseException, not HttpResponseForbidden
我该怎么做呢?
发布于 2011-07-08 09:20:54
从视图中返回它,就像返回任何其他响应一样。
from django.http import HttpResponseForbidden
return HttpResponseForbidden()
发布于 2011-12-03 07:32:07
如果你想引发一个异常,你可以使用:
from django.core.exceptions import PermissionDenied
def your_view(...):
raise PermissionDenied()
文档如下所示:
https://docs.djangoproject.com/en/stable/ref/views/#the-403-http-forbidden-view
与重新获取HttpResponseForbidden
相反,引发PermissionDenied
会导致使用403.html
模板呈现错误,或者您可以使用中间件显示自定义的“禁止”视图。
发布于 2018-12-06 01:29:59
尝试这种方式,发送包含错误的消息
from django.core.exceptions import PermissionDenied
raise PermissionDenied("You do not have permission to Enter Clients in Other Company, Be Careful")
https://stackoverflow.com/questions/6618939
复制相似问题