在Django中,如何检查用户是否在site_id中(site_id是在之后创建的)

huangapple go评论65阅读模式
英文:

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)

&lt;h1&gt;{{ user.site_id }}&lt;/h1&gt;
    &lt;p&gt;---&lt;/p&gt;
    &lt;h1&gt;{{ is_siteone }}&lt;/h1&gt;

{#    {% 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 == &quot;siteone&quot; if user.site_id else False

I specify that my context is like this, so I don't add (request.user) in the template

&quot;is_siteone&quot;: 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) %}
    &lt;!-- some content --&gt;
{% endif %}

huangapple
  • 本文由 发表于 2023年3月7日 22:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定