Django 模块的用户权限
我的 Django 模板中的权限有一个小问题。
我试图根据权限在我的项目的菜单栏中显示一个图标。我希望拥有它,以便如果用户有权向项目添加新的后续项目,他们可以看到该图标,如果他们没有该权限,则不显示链接。
我的权限语法是 follow.add_followup
,这是我通过打印 user.get_all_permissions()
获得的。
我在模板中尝试了此代码:
...
{% if user.has_perm('followup.add_followup') %}
<li><a href="{% url followup-new p.id %}">Log</a></li>
{% endif %}
...
但是当我显示模板时,出现此错误:
模板语法错误位于/project/232/view/
无法解析剩余部分:“user.has_perm(followup.add_followup)”中的“(followup.add_followup)”
有什么想法吗?这一直让我很头疼! :)
I'm having a small issue with my permissions in my Django template.
I'm trying to, based on permissions, show an icon in the menu bar for my project. I want to have it so that if the user has the permissions to add a new follow-up to the project, they can see the icon, if they don't have that permission, then do not display the link.
My permission syntax is follow.add_followup
, which I got from printing user.get_all_permissions()
.
I have tried this code in my template:
...
{% if user.has_perm('followup.add_followup') %}
<li><a href="{% url followup-new p.id %}">Log</a></li>
{% endif %}
...
But when I display the template, I am presented with this error:
TemplateSyntaxError at /project/232/view/
Could not parse the remainder: '(followup.add_followup)' from 'user.has_perm(followup.add_followup)'
Any thoughts? This has been giving me a headache! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您使用的是 Django 权限系统,因此最好使用以下模板语法...
编辑:Django 会自动为每个模型创建 3 个权限,“添加”、“更改”和“删除”。如果不存在添加链接的模型,则必须在模型类Meta中添加相关模型的权限...同样:
somemodels.py
在Django auth用户管理页面中,您可以查看您的许可。在模板层中,权限以基本的 Django 风格呈现,
在本例中,将类似于:
如果没有与您想要执行的工作相关的模型,则将权限添加到模型
中 ...在您的模板中,您可以写入
以密封该用户的可用权限,其中
somemodel
是您将权限添加到其模型之一的应用程序的名称。Since you are using the Django permission system, it's better you use the followingg template syntax...
EDIT: Django automatically creates 3 permissions for each model, 'add', 'change' and 'delete'. If there exists no model for adding a link, then you must add the permission from related model, in the model class Meta... Likewise:
somemodels.py
In the Django auth user admin page, you can see your permission. In the template layer, permission is presented with the basic Django style,
which, in this case, will be like:
If there is no model, related to the job you wish to do, the add the permission to a model...
In your template, you can write
to seal available permissions to that user, where
somemodel
is the name of the applicaton that you add your permission to one of its models.Django 文档详细说明了答案#2:
https://docs.djangoproject.com/en/dev/topics/auth/ #id9
Django documentation detailing answer #2:
https://docs.djangoproject.com/en/dev/topics/auth/#id9
这是我非常简单的解决方案,在您的模板中添加以下内容:
例如:
This is my very simple solution, in your template add this:
for example:
这种复杂的决策在视图函数中进行。
或者它进入上下文,然后呈现给模板。
https://stackoverflow.com/search?q=%5Bdjango%5D+context
何时使用上下文处理器
在您的视图中执行此操作
那么您的模板就很简单
This kind of complex decision-making goes in the view functions.
Or it goes into the context which is then presented to the template.
https://stackoverflow.com/search?q=%5Bdjango%5D+context
When to use context processor
Do this in your view
Then your template is simply