英文:
In Django, how to check if user is in site_id (site_id are created by after)
问题
以下是已经翻译好的内容:
这里我使用分组进行:
views.py
def is_admin(user):
return user.groups.filter(id=1).exists()
def is_client(user):
return user.groups.filter(id=4).exists()
template (main_list.html)
{% for group in request.user.groups.all %}
{% if 1 == group.id %}
而我想使用我的site_id(外键)来实现:
这是我的第一次尝试:
views.py
def is_siteone(user):
return user.site_id.filter(id=1).exists()
template (main_list.html)
{% if is_siteone %}
models.py
class Site(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=255)
version = models.CharField(verbose_name='Program version', blank=False, default='1.0', max_length=20)
def __str__(self):
return self.name
def filter(self, id):
pass
但它返回以下错误
错误信息
你有任何关于如何继续操作的想法吗?
我的site_id是Django的auth_user表中的Site表的外键。
英文:
the objective of the system is to check if the user is in such site_id or not to display a part of the html page to him
here I do it with groups :
views.py
def is_admin(user):
return user.groups.filter(id=1).exists()
def is_client(user):
return user.groups.filter(id=4).exists()
template (main_list.html)
{% for group in request.user.groups.all %}
{% if 1 == group.id %}
and I would like to do it with my site_id (Foreign Key)
here is my first attempt :
views.py
def is_siteone(user):
return user.site_id.filter(id=1).exists()
template (main_list.html)
{% if is_siteone %}
models.py
class Site(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=255)
version = models.CharField(verbose_name='Program version', blank=False, default='1.0', max_length=20)
def __str__(self):
return self.name
def filter(self, id):
pass
but it returns the following error
error return
Do you have any ideas on how to proceed?
My site_id is a Foreign key of a Site table in table auth_user of Django
答案1
得分: 1
以下是已翻译的代码部分:
views.py
def is_siteone(user):
return user.site_id == 1 if user.site_id else False
template (main_list.html)
<h1>{{ user.site_id }}</h1>
<p>---</p>
<h1>{{ is_siteone }}</h1>
{# {% for user in request.user.site.id %}#}
{% if is_siteone %}
so I modified my code in views.py like this:
def is_siteone(user):
return user.site_id == "siteone" if user.site_id else False
I specify that my context is like this, so I don't add (request.user) in the template
"is_siteone": is_siteone(request.user),
英文:
Here is what I was able to do:
views.py
def is_siteone(user):
return user.site_id == 1 if user.site_id else False
template (main_list.html)
<h1>{{ user.site_id }}</h1>
<p>---</p>
<h1>{{ is_siteone }}</h1>
{# {% for user in request.user.site.id %}#}
{% if is_siteone %}
my balise {{ user.site_id }} return siteone in text and my balise {{ is_siteone }} return False
so I modified my code in views.py like this:
def is_siteone(user):
return user.site_id == "siteone" if user.site_id else False
I specify that my context is like this, so I don't add (request.user) in the template
"is_siteone": is_siteone(request.user),
答案2
得分: 0
似乎您正试图直接从“user”对象中访问“site_id”属性,但“site_id”不是“User”模型的直接属性。相反,它是将“User”模型与“Site”模型关联的外键,因此尝试像这样操作:
def is_siteone(user):
return user.site_id == 1 if user.site_id else False
然后在模板中,像这样使用该函数:
{% if is_siteone(request.user) %}
<!-- 一些内容 -->
{% endif %}
英文:
It seems like you are trying to access the site_id
attribute directly from the user
object, but site_id
is not a direct attribute of the User
model. Instead, it is a foreign key that relates the User
model with the Site
model, so try to do something like:
def is_siteone(user):
return user.site_id == 1 if user.site_id else False
Then in the template, use the function like this:
{% if is_siteone(request.user) %}
<!-- some content -->
{% endif %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论