[英]How to display something in a django template according to which user is logged in?
I am displaying a table in a django template, and I have 2 identical templates but with 2 different tables.我在 django 模板中显示一个表格,我有 2 个相同的模板,但有 2 个不同的表格。 I have 2 different user groups.
我有 2 个不同的用户组。 I want to display a different template according to which auth group the user belongs to.
我想根据用户所属的身份验证组显示不同的模板。 For example:
例如:
if user is in group A: render template1.html else if user is in group B: render template2.html如果用户在 A 组中:渲染 template1.html 否则如果用户在 B 组中:渲染 template2.html
All I know right now is that I used the @login_required decorator, so the view is not gonna be shown if the user is not logged in. But this includes all users, and is not specific to groups.我现在所知道的是我使用了@login_required 装饰器,因此如果用户未登录,则不会显示视图。但这包括所有用户,而不是特定于组。
def home(request):
ecv_count = Dossier.objects.filter(status_admin='ECV').count()
v_count = Dossier.objects.filter(status_admin='V').count()
r_count = Dossier.objects.filter(status_admin='R').count()
c_count = Dossier.objects.filter(status_admin='C').count()
context = {
'dossiers': Dossier.objects.all(),
'ecv_count': ecv_count,
'v_count': v_count,
'r_count': r_count,
'c_count': c_count
}
return render(request, 'dashboard/home.html', context)
I want to have the view check the user group, and render a different template with a different context.我想让视图检查用户组,并使用不同的上下文呈现不同的模板。
You can check like this:你可以这样检查:
def home(request):
if request.user.groups.filter(name='some_group').exists():
# do something ....
else:
#do somethig...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.