【发布时间】:2014-10-12 03:49:47
【问题描述】:
每当不允许用户访问任何页面时,我都会使用 django 的 PermissionDenied 来呈现 403.html。
有很多不同类型的页面,例如Product page、User Page、User Contact information、Owner Information。
我想添加带有PermissionDenied 的自定义消息,这将告诉用户他无法查看此页面的确切原因。我想将以下动态消息添加到403.html。
You have are trying to `View a Product (id:3094384)` while having a `Trail` account. You are not authorized to view this product.
和
You have are trying to `View a Customer (id:48)` which is Private. You are not authorized to view this User.
等等。
这是我的代码
elif role.id == Project.ROLE_SALES and not project.sales_person_id == user_id:
raise PermissionDenied
html
<body class="error-page">
<!-- content -->
<section>
<div class="error403">
<h1>403</h1>
</div>
<p class="description">Oops! Request forbidden...</p>
<p>Sorry, it appears the page you were looking for is forbidden and not accessible. If the problem persists, please
contact web Administrator.</p>
# HERE I WANT TO SHOW DYNAMIC MESSAGE.
<a href="{{ request.META.HTTP_REFERER }}" class="btn btn-danger403 btn-primary btn-large" >
Go Back </a>
{{ except }}
</section>
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.js' %}"></script>
</body>
可能性
raise PermissionDenied("Custom message")
或者
将上下文传递给PermissionDenied?
建议。
【问题讨论】:
-
是的,我有。我能够呈现 403.html,但那是静态的,对于所有 premissiondenied 响应都是一样的。我想发送一些关于请求类型的参数(上下文)并显示不同的消息。
-
你能解决这个问题@Clayton 吗?
-
是的。我能够@rka
标签: python django views decorator permission-denied