如何根据用户组更改 Django 模板? [英] How do I change a Django Template based on the User's Group?

查看:25
本文介绍了如何根据用户组更改 Django 模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的网站上有两个不同的用户组:客户和企业.

Right now I have two different groups of users on my site: customers and businesses.

现在我只使用一个登录,允许两个用户组查看他们的个人资料页面.

Right now I am only using one login that allows both user groups to see their profile page.

但是,个人资料页面的某些部分我只希望客户看到,而部分我只希望企业看到.如何限制每个组在此页面上看到的内容?

However, there are portions of the profile page I only want the Customer to see and portions I only want the business to see. How can I go about limiting what each group sees on this page?

我应该在模板中用某种 if 语句来做吗?或者有没有其他人可以让我知道的其他解决方案?

Should I do it in the template with some sort of if statement? or is there some other solution anyone can let me know about?

推荐答案

这对你来说可能太老了,无法再关心了,但在我自己弄清楚之前,我自己偶然发现了这里.为了后代,我找到了以下解决方案:

This is probably too old for you to care any more, but I stumbled here myself before figuring it out on my own. For posterity, I found the following solution:

在您看来,添加如下内容:

In your view, add something like this:

is_customer = request.user.groups.filter(name='Customers').exists()

在您的模板中:
<代码>{% if is_customer %} 客户的东西在这里{% endif %}

它依赖于这样一个事实,即模板中的 if 子句对于空列表将被评估为 false.

It relies on the fact that an if clause in a template will be evaluate to false for an empty list.

这篇关于如何根据用户组更改 Django 模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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