auth.user.none 和 <QuerySet []> 在 ManyToManyField 渲染中

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

auth.user.none and <QuerySet []> in ManyToManyField rendering

问题

我尝试在我的Django网站上显示喜欢某张图片的用户数量。

图片模型如下所示:

class Image(models.Model):
    (***)
    users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,
                        related_name='images_liked',
                        blank=True)

当我在模板中使用以下代码时:

{{ image.users_like.all }}

我得到:

QuerySet []

当我在模板中使用以下代码时:

{{ image.users_like }}

我得到

auth.User.None

这很奇怪,因为在我的管理页面上,有信息显示有人喜欢这张照片。

以下是我的视图函数:

def image_detail(request, id, slug):
    image = get_object_or_404(Image, id=id, slug=slug)
    return render(request,
              'images/image/detail.html',
              {'section': 'images',
              'image': image})

我的管理页面中,用户喜欢部分显示已经喜欢这张照片的用户的用户名。如下截图:

auth.user.none 和 <QuerySet []> 在 ManyToManyField 渲染中

完整代码块如下:

<h1>{{ image.title }}</h1>
<p>{{ image.users_like.all }}</p>
{% load thumbnail %}
{% thumbnail image.image "300" as im %}
    <a href="{{ image.image.url }}">
        <img src="{{ im.url }}" class="image-detail">
    </a>
{% endthumbnail %}
{% with total_likes=image.users_like.count users_like=image.users_like.all %}
    <div class="image-info">
            <div>
                <span class="count">
                    <span class="total">{{ total_likes }}</span>
                    like{{ total_likes|pluralize }}
                </span>
                <a href="#" data-id="{{ image.id }}" data-action="{% if request.user in users_like %}un{% endif %}like" class="like button">
                    {% if request.user not in users_like %}
                        Like
                    {% else %}
                        Unlike
                    {% endif %}
                </a>
            </div>
        {{ image.description|linebreaks }}
    </div>

    <div class="image-likes">
        {% for user in image.users_like.all %}
            <div>
                <img src="{{ user.profile.photo.url }}">
                <p>{{ user.first_name }}</p>
            </div>
        {% empty %}
            Nobody likes this image yet.
        {% endfor %}
    </div>
{% endwith %}
英文:

I try to display numbers of Users who liked some image on my website written in Django.
The Image model looks like this below:<br>

class Image(models.Model):
    (***)
    users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,
                        related_name=&#39;images_liked&#39;,
                        blank=True)

When I use in my template:
>{{image.users_like.all}}

I get :

QuerySet [],

when I use in templates
>{{image.users_like}}

I get

auth.User.None.

It's weird because in my admin page I have information that someone liked this photo.

Below my view function:

def image_detail(request, id, slug):
    image = get_object_or_404(Image, id=id, slug=slug)
    return render(request,
              &#39;images/image/detail.html&#39;,
              {&#39;section&#39;: &#39;images&#39;,
              &#39;image&#39;: image})

EDIT

My admin page, in Users like section display usernames of user who already liked the photo. PrtSc below:<br/>
auth.user.none 和 <QuerySet []> 在 ManyToManyField 渲染中

FULL BLOCK

&lt;h1&gt;{{ image.title }}&lt;/h1&gt;
`&lt;p&gt;{{image.users_like.all}}&lt;/p&gt;
{% load thumbnail %}
{% thumbnail image.image &quot;300&quot; as im %}
    &lt;a href=&quot;{{ image.image.url }}&quot;&gt;
        &lt;img src=&quot;{{ im.url }}&quot; class=&quot;image-detail&quot;&gt;
    &lt;/a&gt;
{% endthumbnail %}
{% with total_likes=image.users_like.count users_like=image.users_like.all %}
    &lt;div class=&quot;image-info&quot;&gt;
            &lt;div&gt;
                &lt;span class=&quot;count&quot;&gt;
                    &lt;span class=&quot;total&quot;&gt;{{ total_likes }}&lt;/span&gt;
                    like{{ total_likes|pluralize }}
                &lt;/span&gt;
                &lt;a href=&quot;#&quot; data-id=&quot;{{ image.id }}&quot; data-action=&quot;{% if request.user in users_like %}un{% endif %}like&quot; class=&quot;like button&quot;&gt;
                    {% if request.user not in users_like %}
                        Like
                    {% else %}
                        Unlike
                    {% endif %}
                &lt;/a&gt;
            &lt;/div&gt;
        {{ image.description|linebreaks }}
    &lt;/div&gt;

    &lt;div class=&quot;image-likes&quot;&gt;
        {% for user in image.users_like.all %}
            &lt;div&gt;
                &lt;img src=&quot;{{ user.profile.photo.url }}&quot;&gt;
                &lt;p&gt;{{ user.first_name }}&lt;/p&gt;
            &lt;/div&gt;
        {% empty %}
            Nobody likes this image yet.
        {% endfor %}
    &lt;/div&gt;
{% endwith %}

答案1

得分: 0

查询集在调用时为空。在将用户添加到image.users_like之后,问题消失了。谢谢solarissmoke。

英文:

Answer in the comments above. QuerySet was empty when called. After adding users to image.users_like the problem disappeared. Thank you solarissmoke.

huangapple
  • 本文由 发表于 2020年1月3日 20:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578574.html
匿名

发表评论

匿名网友

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

确定