【发布时间】:2021-10-27 21:39:21
【问题描述】:
我正在尝试制作一个只有结果所属的用户才能看到的页面。所以我想让只有user_name_id=1(和超级用户)的用户才能看到localhost:8000/mpa/sum/1/的页面
我在 html 中试过这个:
{% if request.user.is_superuser %}
<div class="container text-justify p-5">
<div class="display-4 text-left text-primary my-3">
...
{% else %}
You are not able to view this page!
{% endif %}
这对超级用户来说很好,但我怎么能对用户这样做呢?
views.py
@login_required
def individual_sum(request, user_name_id):
... lots of query
context = {
... lots of contexts
}
return render(request, 'stressz/individual_sum.html', context)
models.py
class IndividualSum_text(models.Model):
def __str__(self):
return str(self.user_name)
user_name = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
...integerfields and textfields here
【问题讨论】:
标签: python django view user-permissions restriction