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

查看:123
本文介绍了如何根据用户组更改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%}

它依赖于一个事实,一个模板中的如果子句将被评估为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天全站免登陆