简体   繁体   中英

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

Thats because you aren't providing any variable called data to the template.

change

return render(request, 'contest.html', data)

by

return render(request, 'contest.html', {'data': data})

You should use key 'badge_recipients' in template:

<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})

Then when you iterate over dictionary you have to do dictionary.items() that was the only thing missing from above answers.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM