python - PermissionRequiredMixin AccessMixinpermission_denied_message

标签 python django

好的,所以我不知道为什么当 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/

相关文章:

python - 获取用户是否被登录用户关注,不起作用

Django REST Framework GET 检索具有 NAME LIKE %NAME% 的行

python - 列表对象没有属性列

python - Pandas 通过事件创建月末持有量

python - 在 GitLab CI YML 文件中使用 pywin32 - 在 pip 安装时出错

python - -bash : ./manage.py:权限被拒绝

django - Upstart 卡在停止

python - Django + OsX Lion + 带有 XAMPP 的 Mysql

python - 计算列表中整数的最优雅方法

python - 如何显示除当前使用 Django 登录的用户之外的所有用户