Mil*_*ano 31 python django django-admin django-permissions
如何检入模板用户是否属于某个组?
有可能在view生成template但是如果我想检查这base.html是一个扩展模板(它没有它自己的视图功能)怎么办?
我的所有模板都扩展了,base.html所以在每个模板中检查它都不好view.
在base.html包含上部杆,它应包含在其中的按钮根据group登录用户是(客户,销售商).
在我的base.html是:
{% if user.is_authenticated %}
Run Code Online (Sandbox Code Playgroud)
这是不够的,因为我必须采取不同的方式对用户Customers和用户采取不同的行动Sellers.
所以我想要的是:
{% if user.in_group('Customers') %}
<p>Customer</p>
{% endif %}
{% if user.in_group('Sellers') %}
<p>Seller</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
mis*_*bah 56
您需要自定义模板标记:
from django import template
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
Run Code Online (Sandbox Code Playgroud)
在您的模板中:
{% if request.user|has_group:"mygroup" %}
<p>User belongs to my group
{% else %}
<p>User doesn't belong to mygroup</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
资料来源:http://www.abidibo.net/blog/2014/05/22/check-if-user-belongs-group-django-templates/
文件:https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/
fus*_*596 12
在您的应用中创建一个文件夹'templatetags'.在此文件夹中创建两个文件:
__init__.py
auth_extras.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):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
Run Code Online (Sandbox Code Playgroud)
现在看起来应该是这样的:
app/
__init__.py
models.py
templatetags/
__init__.py
auth_extras.py
views.py
Run Code Online (Sandbox Code Playgroud)
添加templatetags模块后,您需要重新启动服务器,然后才能在模板中使用标记或过滤器.
在base.html(模板)中使用以下内容:
{% load auth_extras %}
Run Code Online (Sandbox Code Playgroud)
并检查用户是否在"主持人"组中:
{% if request.user|has_group:"moderator" %}
<p>moderator</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
文档:https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/
注意如果数据库中不存在该组,您将获得异常.
自定义模板标记应为:
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):
try:
group = Group.objects.get(name=group_name)
except Group.DoesNotExist:
return False
return group in user.groups.all()
Run Code Online (Sandbox Code Playgroud)
你的模板:
{% if request.user|has_group:"mygroup" %}
<p>User belongs to my group
{% else %}
<p>User doesn't belong to mygroup</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
我会说最好的方法是:
yourapp/templatetags/templatetagname.py
from django import template
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
Run Code Online (Sandbox Code Playgroud)
yourapp /模板/ yourapp/yourtemplate.html:
{% load has_group %}
{% if request.user|has_group:"mygroup" %}
<p>User belongs to my group
{% else %}
<p>User doesn't belong to mygroup</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
编辑:添加了带有模板标记加载的行,如评论中所建议的那样.
小智 7
在您的模板中
{% ifequal user.groups.all.0.name "user" %}
This is User
{% endifequal %}
Run Code Online (Sandbox Code Playgroud)
你可以使用这个:
{% for group_for in request.user.groups.all %}
{% if group_for.name == 'Customers' %}
Text showed to users in group 'Customers'
{% elif group_for.name == 'Sellers' %}
Text showed to users in group 'Sellers'
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这是迭代与发出请求的用户相关的组,如果迭代组的名称等于“客户”、“卖家”等,则打印文本
我发现的最简单的方法是使用 a 将所有组名称添加到上下文中context_preprocessor
在您的应用程序中创建一个文件context_processors.py并添加以下内容:
def user_groups_processor(request):
groups = []
user = request.user
if user.is_authenticated:
groups = list(user.groups.values_list('name',flat = True))
return {'groups': groups}
Run Code Online (Sandbox Code Playgroud)
在您的设置中,添加新的上下文处理器
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
"context_processors": [
"my_app.context_processors.user_groups_processor"
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
或者如果您更喜欢settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
"context_processors": [
"my_app.context_processors.user_groups_processor"
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
之后,您可以在模板中使用:
{% if 'vip' in groups %}
<p>Paragraph only visible to VIPs</p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18977 次 |
| 最近记录: |