好的,所以我不知道为什么当 PermissionDenied 异常引发并且还插入了 Permission_denied_message 变量时,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.html
in 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 的模板中可用。
另请注意,您可能并不总是希望公开异常的内容!
关于python - PermissionRequiredMixin AccessMixinpermission_denied_message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37016266/