在Django中,如何根据department_name限制用户对模板的访问.每个用户属于某个部门



我有下面的模型。部门、用户。用户被分配到一个部门。如何根据用户的department_name限制模板的访问权限?例如:如果department_name == "Computer_Department",用户可以查看Application Template_1。这里的用户属于"计算机"。部门。如果department_name == "Electrical_Department",则用户可以查看"Application Template_2"。此处用户属于"电气"

* * * * * *我的代码如下models.py

class Departments(models.Model):
id = models.AutoField(primary_key=True)
department_name = models.CharField(max_length=255) # here department name can be computer, electrical etc
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = models.Manager()
class Users(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete = models.CASCADE)
department_id = models.ForeignKey(Departments, on_delete=models.DO_NOTHING, default=1)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = models.Manager()

UserViews.py

def bi_powise(request):
return render ( request, 'user_template/application_template/bi_powise.html', {})

urls . py

path ( 'bi_powise_user', UserViews.bi_powise, name = 'bi_powise_user' )

sidebar_template.html

{% url 'bi_powise_user' as bi_powise_user %}
<a href="{{ bi_powise_user }}" class="nav-link {% if request.path == bi_powise_user %} active {% endif %}">
<i class="nav-icon fas fa-chalkboard"></i>
<p>
BI PO-Wise
</p>
</a>
</li>

bi_powise.html

{% extends 'user_template/base_template.html' %}
{% block page_title %}
ANSwer BI PO-WISE  
{% endblock page_title %}
{% block main_content %}
<!-- Main content -->
<body>
<section class="content">
<center>
<iframe width="1400" height="1300" src="https://app.powerbi.com/reportEmbed?reportId=****************************************" frameborder="0" allowFullScreen="true"></iframe>
</center>

<!-- </div> -->

</section>
</body>

{% endblock %}

你可以直接在视图中添加逻辑,或者编写一个装饰器来执行权限。例如,请查看django认证文档。

相关内容

  • 没有找到相关文章

最新更新