使用視圖我已生成以下字典結構:django的模板:渲染字典內的字典和元件
{'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'}}
這裏是我所產生的字典(只包括我構建字典其中的部分)
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})
在我的模板我試圖使其如下:
<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>
但我得到錯誤說數據沒有定義,也沒有得到渲染。基於提供的答案,我做了更改,現在我的錯誤如下:'builtin_function_or_method' object is not iterable
我得到這個錯誤:「builtin_function_or_method」對象不是可迭代 –
您可以編輯您的文章,並顯示現在怎麼看你的代碼? –
只是編輯它。 –