我想做以下事情:
raise HttpResponseForbidden()
但是我得到了错误:
exceptions must be old-style classes or derived from BaseException, not HttpResponseForbidden
我应该怎么做?
最佳答案
如果你想抛出异常,你可以使用:
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
模板呈现错误,或者您可以使用中间件显示自定义“禁止” View 。
关于django - 我如何在 django 中提出禁止响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6618939/