在模板中获取用户组 [英] Get user group in a template

查看:130
本文介绍了在模板中获取用户组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示根据当前登录用户的用户组进行更改的菜单,此逻辑位于我的视图之内,然后设置一个变量来检入模板以确定要显示的菜单项。 ...我以前问过这个问题,但我的逻辑正在模板中完成。所以现在我想要它在我看来...菜单看起来如下

 < ul class =sidemenu> 
< li>< a href =/>常规列表< / a>< / li>
< li>< a href =/ sales_list>销售列表< / a>< / li>
< li>< a href =/ add_vehicle>添加新记录< / a>< / li>
< li>< a href =/ edit_vehicle>编辑现有记录< / a>< / li>
< li>< a href =/ filter>过滤记录< / a>< / li>
< li>< a href =/ logout>注销< / a>< / li>
< / ul>

根据用户管理,他们会看到一切...但假设用户在集团销售,他们只会看到前两个和最后两个项目...等等。我还需要基于用户组登录后的动态重定向。任何想法?

解决方案

标准的Django检查权限的方式是单独的权限标志,而不是测试组名。 / p>

如果您必须检查组名称,请注意,用户到组是一对多关系,您可以获得第一个组您的模板中的组列表如下:

  {{user.groups.all.0}} 

或者在条件(未经测试但应该工作)中使用它:

  {%ifequal user.groups.all.0'Sales'%} 
...
{%endif%}

如果您使用首选权限模型,则可以执行以下操作。

  ... 

{%如果perms.vehicle.can_add_vehicle%}
< li>< a href = / add_vehicle>添加新记录< / a>< / li>
{%endif%}
{%如果perms.vehicle.can_change_vehicle%}
< li>< a href =/ edit_vehicle>编辑现有记录< / a> < /锂>
{%endif%}

...

这些假设您的应用程序被称为车辆,则该模型被称为 syncdb c>车辆。



如果用户是超级用户,他们将自动拥有所有权限。



如果用户在销售组中,他们将不具有这些车辆权限(除非您已将这些权限添加到该组中)。



如果用户在管理组中,他们可以拥有这些权限,但您需要将其添加到Django管理站点中的组。



对于您的其他问题,请在登录时重定向基于用户组:用户到组是一个多对多关系,所以使用它像一对多不是一个好主意。


I want to display a menu that changes according to the user group of the currently logged in user, with this logic being inside of my view, and then set a variable to check in the template to determine which menu items to show....I'd asked this question before, but my logic was being done in the template. So now I want it in my view...The menu looks as below

   <ul class="sidemenu">
    <li><a href="/">General List </a></li>
    <li><a href="/sales_list">Sales List </a></li>
    <li><a href="/add_vehicle">Add a New Record </a></li>
    <li><a href="/edit_vehicle">Edit Existing Record </a></li>
    <li><a href="/filter">Filter Records </a></li>
    <li><a href="/logout">Logout </a></li>
  </ul>

Suppossing the user is management, they'll see everything...But assuming the user is in the group sales, they'll only see the first two and the last two items...and so on. I also want a dynamic redirect after login based on the user's group. Any ideas?

解决方案

The standard Django way of checking permissions is by the individual permission flags rather than testing for the group name.

If you must check group names, being aware that Users to Groups is a many-to-many relationship, you can get the first group in the list of groups in your template with something like this:

{{ user.groups.all.0 }}

or using it like this in a conditional (untested but should work):

{% ifequal user.groups.all.0 'Sales' %}
   ...
{% endif %}

If you go with the preferred permission model you would do something like the following.

...

  {% if perms.vehicle.can_add_vehicle %}
    <li><a href="/add_vehicle">Add a New Record </a></li>
  {% endif %}
  {% if perms.vehicle.can_change_vehicle %}
    <li><a href="/edit_vehicle">Edit Existing Record </a></li>
  {% endif %}

...

These are the permissions automatically created for you by syncdb assuming your app is called vehicle and the model is called Vehicle.

If the user is a superuser they automatically have all permissions.

If the user is in a Sales group they won't have those vehicle permissions (unless you've added those to the group of course).

If the user is in a Management group they can have those permissions, but you need to add them to the group in the Django admin site.

For your other question, redirect on login based on user group: Users to Groups is a many-to-many relationship so it's not really a good idea to use it like a one-to-many.

这篇关于在模板中获取用户组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆