[英]django template: Render dictionary within dictionary and elements
Using view I have generated following dictionary structure: 使用视图,我生成了以下字典结构:
{'badge-bronze': {'badge_recipients': [<User: aa>, <User: ni>], 'badge': 'badge-bronze', 'page_class': 'meta'}, 'badge-silver': {'badge_recipients': [<User: ni>], 'badge': 'badge-silver', 'page_class': 'meta'}, 'badge-gold': {'badge_recipients': [<User: ni>], 'badge': 'badge-gold', 'page_class': 'meta'}}
Here is how I generated the dictionary (only included the part where I construct the dictionary) 这是我生成字典的方式(仅包括构建字典的部分)
def contest(request):
all_badges = badge_data.BADGES.keys()
data = {}
for badge in all_badges:
if badge == 'badge-bronze' or badge == 'badge-silver' or badge == 'badge-gold':
badge_data_info = get_object_or_404(BadgeData, slug=badge)
badge_recipients = User.objects.filter(
award_user__badge = badge_data_info
).annotate(
last_awarded_at = Max('award_user__awarded_at'),
award_count = Count('award_user')
).order_by(
'-last_awarded_at'
)
data[badge] = {
'badge_recipients' : badge_recipients,
'badge' : badge_data_info,
'page_class': 'meta',
}
#import pdb
#pdb.set_trace()
return render(request, 'contest.html', {'data':data})
In my template I tried to render it as follows: 在我的模板中,我尝试将其渲染如下:
<div id="award-list">
{% for key,value in data.items %}
<li>key</li>
{% for recipient in value.badge_recipients %}
<div class="user">
<span class="thumb">{{ gravatar(recipient, 32) }}</span>
<span><a href="{{ recipient.get_absolute_url() }}">{{recipient.username|escape}}</a></span>
<span>{{ macros.user_score_and_badge_summary(user) }}</span>
</div>
{% endfor %}
{% endfor %}
</div>
but i get error saying data is not define and also nothing get render. 但我收到错误消息,说数据未定义,也没有得到渲染。 Based on the answers provided I made the change and now my error is follows:
'builtin_function_or_method' object is not iterable
根据提供的答案,我进行了更改,现在我的错误如下:
'builtin_function_or_method' object is not iterable
Thats because you aren't providing any variable called data
to the template. 那是因为您没有向模板提供任何名为
data
变量。
change 更改
return render(request, 'contest.html', data)
by 通过
return render(request, 'contest.html', {'data': data})
You should use key 'badge_recipients' in template: 您应该在模板中使用键“ badge_recipients”:
<div id="award-list">
{% for key,value in data.items %}
<li>key</li>
{% for recipient in value.badge_recipients %}
<div class="user">
<span class="thumb">{{ gravatar(recipient, 32) }}</span>
<span><a href="{{ recipient.get_absolute_url() }}">{{recipient.username|escape}}</a></span>
<span>{{ macros.user_score_and_badge_summary(user) }}</span>
</div>
{% endfor %}
{% endfor %}
</div
With the help of above answers I managed to figure out the solution for this question: 在以上答案的帮助下,我设法找到了该问题的解决方案:
First fix was: 第一个解决方法是:
return render(request, 'contest.html', data)
to return render(request, 'contest.html', {'data': data})
return render(request, 'contest.html', data)
以return render(request, 'contest.html', {'data': data})
Then when you iterate over dictionary you have to do dictionary.items()
that was the only thing missing from above answers. 然后,当您遍历字典时,您必须执行
dictionary.items()
,这是上面答案中唯一缺少的东西。
<div id="award-list">
{% for key,value in draw.items() %}
{% for recipient in value.badge_recipients %}
<div class="user">
<span class="thumb">{{ gravatar(recipient, 32) }}</span>
<span><a href="{{ recipient.get_absolute_url() }}">{{recipient.username|escape}}</a></span>
<span>{{ macros.user_score_and_badge_summary(user) }}</span>
</div>
{% endfor %}
{% endfor %}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.