I have a Django View that constructs a dictionary to a template. I have seen similar questions but no one shows how to access the dictionary value in the template using the object pk
as the key (in my case the key
s are pk
s of the object).
View code that constructs the dict:
comment_开发者_开发知识库uservote = {}
if not current_logged_user.is_anonymous():
for comment in comments_all:
try:
co_vote = Vote.objects.get(user=current_logged_user, comment=comment)
comment_uservote[comment.id] = co_vote.vote
except Vote.DoesNotExist:
co_vote = ''
comment_uservote[comment.id] = co_vote
I have also tried with comment_uservote[str(comment.id)]
but this does not help either.
Template (that does not work):
{% for comment in comments %}
{{comment_uservote.comment.pk}} <!--this does not work-->
{% enfor %}
However, the following works if I add any comment's pk
to the comment_uservote
.
Template (that works but if a direct substitution):
{% for comment in comments %}
{{comment_uservote.16}} <!--this works-->
{% enfor %}
Appreciate your help. Please let me know if you need something more from me.
No, this doesn't work, and the documentation doesn't imply that it should. You will need a custom tag or filter.
精彩评论