好的,所以我不知道为什么在插入 permission_denied_message 变量的情况下引发 PermissionDenied 异常时 django 显示“403 Forbidden”。我想显示我自己的自定义消息。虽然不确定做错了什么。这是我的代码
class ProfileFormView(PermissionRequiredMixin, generic.UpdateView):
permission_required = 'shop_buy.change_userprofile'
raise_exception = True
permission_denied_message = 'Permission Denied'
model = UserProfile
fields = ['location', 'gender', 'profile_picture']
template_name_suffix = '_form'
请您参考如下方法:
来自documentation :
Django has a view to handle 403 Forbidden errors... This view loads and renders the template
403.htmlin your root template directory, or if this file does not exist, instead serves the text “403 Forbidden”, as per RFC 7231#section-6.5.3 (the HTTP 1.1 Specification).
所以默认响应只有两个词:“403 Forbidden”——它不显示异常消息。如果你想显示异常(在 View 上下文中可用),你需要提供你自己的 403.html 模板来显示它。这样的事情会起作用:
<h1>403 Forbidden</h1>
<p>{{ exception }}</p>
请注意,该异常仅在 Django 1.9 的模板中可用。
另请注意,您可能并不总是希望公开异常的内容!
