Issue
{% for blog in data %}
{% with current_ids=blog.blog_id %}
{{ like_ids }}
{{ current_ids }}
{{ like_ids['bar'] }}
{% endwith %}
{% endfor %}
in above code {{ like_ids }} prints {'foo': [], 'bar': [<User: [email protected]>]} {{ current_ids }} prints foo || bar {{ like_ids['bar'] }} throws error "Could not parse the remainder: '['bar']' from 'like_ids['bar']'"
i tried like_ids.bar and it prints [<User: [email protected]>] but, like_ids.current_ids doesn't print anything
tried set instead of with and the problem still exists
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)
This is my views.py code
Solution
Change the function,
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)
To below.
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)
Change template as below.
{% for blog in data %}
{% for like in blog.bloglike_set.all %}
{{ like.id }}
{% endfor %}
{{ blog.id }}
{% endfor %}
Template explanation.
{% 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 %}
Answered By - Pycm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.