1👍
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),
👤ES'
0👍
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 %}
- [Answered ]-How to invert django models.BooleanField's value?
- [Answered ]-Django: How to use model properties within custom methods in a serializer?
Source:stackexchange.com