【问题标题】:django template: Render dictionary within dictionary and elementsdjango 模板:在字典和元素中渲染字典
【发布时间】:2014-05-12 18:56:49
【问题描述】:

使用视图我生成了以下字典结构:

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

【问题讨论】:

    标签: python django django-templates jinja2


    【解决方案1】:

    那是因为您没有向模板提供任何名为 data 的变量。

    改变

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

    通过

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

    【讨论】:

    • 我收到这个错误:'builtin_function_or_method' 对象不可迭代
    • 您可以编辑您的帖子并显示您的代码现在的外观吗?
    • badges_recipients 中的某些可能是None?
    • 不正确,因为如果徽章存在,我正在填充字典......基于 if 条件......但我刚刚更新了问题......
    • 显然你没有得到你所期望的badge_recipients。你调试过代码吗?
    【解决方案2】:

    您应该在模板中使用密钥“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
    

    【讨论】:

    • 我尝试了两个答案,但它并没有真正起作用。当我尝试您的答案时,我收到错误消息说数据未定义。
    • 你应该像 Miranda 写的那样将数据传递给模板,我忘了。
    • 我真的用你们的答案编辑了问题吗..'builtin_function_or_method' 对象不可迭代
    • 展示如何在视图函数中生成 badge_recipients 变量
    • 在这种情况下,您应该使用 {% for recipient in value.badge_recipients.all %}
    【解决方案3】:

    在上述答案的帮助下,我设法找出了这个问题的解决方案:

    第一个修复是:

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

    然后,当您遍历字典时,您必须执行 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>
    

    【讨论】:

    • 哇...我们怎么错过了??为“鹰眼”+1
    • @RaydelMiranda 这个答案是一个协作的努力,所以我不确定我是否应该将此标记为答案。我会保持原样。 :)
    猜你喜欢
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2014-08-25
    • 2011-05-16
    相关资源
    最近更新 更多