更改功能,
def bloglist(request):
like_ids = {}
blog_data = Blog.objects.select_related('published_by').filter(status='Published').all()
for blog in blog_data:
blog.comment = blog.blogcomment_set.select_related('commented_by').all()
blog.like = blog.bloglike_set.select_related('liked_by').all()
like_ids[blog.blog_id] = [like.liked_by for like in blog.like]
args = {
'data': blog_data,
'like_ids': like_ids
}
print(args)
return render(request, 'bloglist.html', args)
到下面。
def bloglist(request):
blog_data = Blog.objects.select_related('published_by').filter(status='Published').all()
args = {
'data': blog_data,
}
print(args)
return render(request, 'bloglist.html', args)
更改模板如下。
{% for blog in data %}
{% for like in blog.bloglike_set.all %}
{{ like.id }}
{% endfor %}
{{ blog.id }}
{% endfor %}
模板说明。
{% for blog in data %}
# ALL THE IDs OF LIKES OF CURRENT BLOG
{% for like in blog.bloglike_set.all %}
{{ like.id }}
{% endfor %}
# CURRENT BLOG ID
{{ blog.id }}
{% endfor %}