英文:
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})
我的管理页面中,用户喜欢部分显示已经喜欢这张照片的用户的用户名。如下截图:
完整代码块如下:
<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='images_liked',
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,
'images/image/detail.html',
{'section': 'images',
'image': image})
EDIT
My admin page, in Users like section display usernames of user who already liked the photo. PrtSc below:<br/>
FULL BLOCK
<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 %}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论