我创建了一个password_change_done
模板。
但我需要Back to Dashboard
为员工和Back to Profile
客户显示一个按钮。
我怎样才能通过用户组检查来实现这一点,而不会弄乱views.py
?
我创建了一个password_change_done
模板。
但我需要Back to Dashboard
为员工和Back to Profile
客户显示一个按钮。
我怎样才能通过用户组检查来实现这一点,而不会弄乱views.py
?
然后你必须使用filter
下面的模板...
在您的应用程序中创建一个文件夹“templatetags”。在此文件夹中创建两个文件:
文件夹结构看起来像......
app/
__init__.py
models.py
templatetags/
__init__.py
get_group.py
views.py
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()
然后在你的html页面中使用它如下......
{% load get_group %}
{% if request.user|has_group:"Client" %}
... Back to Dashboard button ...
{% else %}
... Back to profile button ...
{% endif %}