[英]How to show different content depending on UserGroup in Django?
I have created a password_change_done
template.我创建了一个
password_change_done
模板。
But I need to to show a Back to Dashboard
button for Employees and a Back to Profile
for Customers.但我需要为员工显示一个
Back to Dashboard
按钮,为客户显示一个Back to Dashboard
Back to Profile
。
How can I achieve this through UserGroup checking, without messing with the views.py
?如何通过 UserGroup 检查实现这一点,而不会弄乱
views.py
?
Then you have to use the filter
of the template as below...然后你必须使用模板的
filter
,如下所示......
In your app create a folder 'templatetags'.在您的应用程序中创建一个文件夹“templatetags”。 In this folder create two files:
在此文件夹中创建两个文件:
The folder structure looks like ...文件夹结构看起来像...
app/
__init__.py
models.py
templatetags/
__init__.py
get_group.py
views.py
get_group.py file : get_group.py 文件:
from django import template
from django.contrib.auth.models import Group
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
Then in your html page use it as below...然后在您的 html 页面中使用它如下...
{% load get_group %}
{% if request.user|has_group:"Client" %}
... Back to Dashboard button ...
{% else %}
... Back to profile button ...
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.