【发布时间】:2020-01-03 17:19:50
【问题描述】:
我需要帮助来对我的 django 项目实施访问控制。有2个主要角色,销售和开发人员。在这两个角色中,还有另一个层次结构,经理和非经理。根据他们的角色,我想显示不同的内容并进行不同类型的查询。
我目前使用的方法是扩展我的用户模型以包含这些角色,并在我的模板中使用 if 语句来相应地显示功能。
这是我的模型:
class UserProfile(models.Model):
role = (
('sm','sales_manager'),
('s','sales'),
('rm','rnd_manager'),
('r','rnd')
)
user = models.OneToOneField(User,on_delete=models.CASCADE)
user_type = models.TextField(max_length=500, choices= role)
contact = models.IntegerField(default=92388112)
def __str__(self):
return str(self.user.username)
这是我的看法:
@login_required(login_url='login')
def rnd/home(request):
print(request.user.username)
context = {
'userProfile' : UserProfile.objects.all(),
}
return render(request, 'rnd/home.html',context)
这是我的模板的相关部分:
{%if user.get_UserProfile.user_type == 's' or user.get_UserProfile.user_type == 'sm' %}
<p>Sales</p>
{%else%}
<p>RnD</p>
{%endif%}
<li>
但是,我的 for 循环不起作用。它不会抛出任何错误,但也不做任何事情。当我以 'r' 类型登录时,销售仍会显示在我的屏幕上。
如果有人能回答我并留下一些关于实现此类访问控制的最佳方式的提示,那就太好了,不仅在功能方面,而且在过滤常见功能中显示的数据方面。
【问题讨论】:
标签: python html django django-templates