Django 模块的用户权限

发布于 2024-10-15 21:30:32 字数 603 浏览 9 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何止钟意 2024-10-22 21:30:32

由于您使用的是 Django 权限系统,因此最好使用以下模板语法...

{%if perms.followup.add_followup%}your URL here{%endif%}

编辑:Django 会自动为每个模型创建 3 个权限,“添加”、“更改”和“删除”。如果不存在添加链接的模型,则必须在模型类Meta中添加相关模型的权限...同样:

somemodels.py

class SomeModel(Model):
    ...
    class Meta:
    permissions = (('add_followup','Can see add urls'),(...))

在Django auth用户管理页面中,您可以查看您的许可。在模板层中,权限以基本的 Django 风格呈现,

<app_label>.<codename>

在本例中,将类似于:

{%if perms.somemodels.add_followup%}your URL here{%endif%}

如果没有与您想要执行的工作相关的模型,则将权限添加到模型

中 ...在您的模板中,您可以写入

{{perms.somemodels}}

以密封该用户的可用权限,其中 somemodel 是您将权限添加到其模型之一的应用程序的名称。

Since you are using the Django permission system, it's better you use the followingg template syntax...

{%if perms.followup.add_followup%}your URL here{%endif%}

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

class SomeModel(Model):
    ...
    class Meta:
    permissions = (('add_followup','Can see add urls'),(...))

In the Django auth user admin page, you can see your permission. In the template layer, permission is presented with the basic Django style,

<app_label>.<codename>

which, in this case, will be like:

{%if perms.somemodels.add_followup%}your URL here{%endif%}

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

{{perms.somemodels}}

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.

旧夏天 2024-10-22 21:30:32

Django 文档详细说明了答案#2:
https://docs.djangoproject.com/en/dev/topics/auth/ #id9

当前登录用户的权限存储在模板变量{{ perms }}中。这是 django.contrib.auth.context_processors.PermWrapper 的一个实例,它是模板友好的权限代理。

Django documentation detailing answer #2:
https://docs.djangoproject.com/en/dev/topics/auth/#id9

The currently logged-in user's permissions are stored in the template variable {{ perms }}. This is an instance of django.contrib.auth.context_processors.PermWrapper, which is a template-friendly proxy of permissions.

人心善变 2024-10-22 21:30:32

这是我非常简单的解决方案,在您的模板中添加以下内容:

例如:

.......

{% if 'user.can_drink' in user.get_all_permissions %}
   {{ user }} can drink.
   .......
{% else %}
   {{ user }} can´t drink.
    ........
{% endif %}

.......

This is my very simple solution, in your template add this:

for example:

.......

{% if 'user.can_drink' in user.get_all_permissions %}
   {{ user }} can drink.
   .......
{% else %}
   {{ user }} can´t drink.
    ........
{% endif %}

.......
眼眸里的那抹悲凉 2024-10-22 21:30:32

我在我的模板中尝试过此代码:

这种复杂的决策在视图函数中进行。

或者它进入上下文,然后呈现给模板。

https://stackoverflow.com/search?q=%5Bdjango%5D+context

何时使用上下文处理器

在您的视图中执行此操作

def my_view( request ):
    followup= user.has_perm('followup.add_followup')
    # etc.
    return render_to_response( template, {'followup':followup,... )

那么您的模板就很简单

{% if followup %}
<li><a href="{% url followup-new p.id %}">Log</a></li>
{% endif %}

I have tried this code in my template:

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

def my_view( request ):
    followup= user.has_perm('followup.add_followup')
    # etc.
    return render_to_response( template, {'followup':followup,... )

Then your template is simply

{% if followup %}
<li><a href="{% url followup-new p.id %}">Log</a></li>
{% endif %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文