A.J*_*.J. 9 python django views decorator permission-denied
每当不允许用户访问任何页面时,我都会使用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.
Run Code Online (Sandbox Code Playgroud)
和
You have are trying to `View a Customer (id:48)` which is Private. You are not authorized to view this User.
Run Code Online (Sandbox Code Playgroud)
等等.
elif role.id == Project.ROLE_SALES and not project.sales_person_id == user_id:
raise PermissionDenied
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
raise PermissionDenied("Custom message")
Run Code Online (Sandbox Code Playgroud)
传递上下文PermissionDenied?
建议.
kvr*_*127 13
这个答案可能很晚才到你.但在这里.您可以在Django代码中使用它:
raise PermissionDenied("Custom message")
Run Code Online (Sandbox Code Playgroud)
然后使用403.html模板中的以下代码段显示自定义消息:
{% if exception %}
<p>{{ exception }}</p>
{% else %}
<p>Static generic message</p>
{% endif %}Run Code Online (Sandbox Code Playgroud)
传递给'PermissionDenied'的消息字符串在模板上下文中可用,如Django文档中所述 - https://docs.djangoproject.com/en/1.10/ref/views/#http-forbidden-view
| 归档时间: |
|
| 查看次数: |
7015 次 |
| 最近记录: |