【问题标题】:Django Iterating over a dictionary which is in a list and showing the data in a templateDjango迭代列表中的字典并在模板中显示数据
【发布时间】:2014-01-13 02:04:59
【问题描述】:

你好,我是新手程序员

def calculateMark(mobile_a, mobile_b):
    mobiles_list = [mobile_a, mobile_b]

    for mobile in mobiles_list:
        dimension = TechSpecificationAdd.objects.filter(mobile_name = mobile).values(dimension)
        body_material = TechSpecificationAdd.objects.filter(mobile_name = mobile).values(body_material)
        weight = TechSpecificationAdd.objects.filter(mobile_name = mobile).values(weight)

        tech_variables = {'dimension' : dimension, 'body_material' : body_material, 'weight' : weight}

        return render_to_response('compare.html', tech_variables)

我有这样的东西,一个移动设备列表,从数据库中分配值,然后在字典中分配变量。我正在考虑遍历字典并在模板中显示值。 但问题是我必须制作模板以在一页中显示两个手机信息以进行比较。如何在模板中一次显示两个手机信息?我认为模板将始终显示一部手机的信息。 其实我有点卡在这里,我不知道现在该怎么办。 我从一开始就错了吗?我需要字典吗?如何迭代或分配值以在模板中显示。还是我在问一个愚蠢的问题?

【问题讨论】:

    标签: python django dictionary django-templates django-views


    【解决方案1】:

    改进 Simeon 的(假设是有效的urls.py),

    from django.shortcuts import render
    
    def calculateMark(request, mobile_a, mobile_b):
        mobiles_list = [mobile_a, mobile_b]
    
        results = []
        for mobile in mobiles_list:
            record = TechSpecificationAdd.objects.filter(mobile_name=mobile).values('dimension', 'body_material', 'weight')
            results += record
    
        return render(request, 'compare.html', {'data': results})
    

    任何注释:

    • 视图函数必须将request 作为第一个参数。
    • 过滤总是返回列表(即使只有结果1),所以{{ record.dimension }}、{{ record.body_material }}和{{ record.weight }}每个都会变成列表。这就是为什么我们使用results += record 而不是results.append(dict),这样{{ record }} 将被适当地呈现。
    • 使用render_to_response 渲染需要RequestContext,而Django 提供django.shortcuts.render 来简化模板渲染。

    【讨论】:

    • 您好 Aditya,感谢您的回答这不是我的主视图函数,此函数从主视图函数调用,因此此处缺少请求参数。我会让你知道你的代码是否有效。如果你有什么想法,请给我建议。
    • Aditya 我有点困惑。我的列表中有两部手机。我必须在一页模板上显示两个移动规范。如果我将 {{ record.weight }} 写入模板,它将是哪个手机的重量?
    • 当然首先是mobile_a,然后是mobile_b(因为mobiles_list = [mobile_a, mobile_b])
    • 嗨 Aditya,如果我对诸如 record.weight = record.weight/100 之类的单个项目进行一些数学运算。之后,如果我按照你所说的那样写这一行,结果 += 记录。会好吗?做数学运算后,“结果”变量会根据项目的新值更新吗?
    • 没问题,阿里。如果你问我django-annoying 的事,你只会惹我生气;-)。
    【解决方案2】:

    我想你打算这样做:

    def calculateMark(mobile_a, mobile_b):
        mobiles_list = [mobile_a, mobile_b]
    
        results = []
        for mobile in mobiles_list:
            dimension = TechSpecificationAdd.objects.filter(mobile_name = mobile).values(dimension)
            body_material = TechSpecificationAdd.objects.filter(mobile_name = mobile).values(body_material)
            weight = TechSpecificationAdd.objects.filter(mobile_name = mobile).values(weight)
    
            results.append({'dimension' : dimension, 'body_material' : body_material, 'weight' : weight})
    
        return render_to_response('compare.html', { 'data': results })
    

    然后你可以在模板中做:

    {% for record in data %}
        {{ record.dimension }}
        {{ record.body_material }}
        {{ record.weight }}
    {% endfor %}
    

    【讨论】:

    • 您好 Simeon 感谢您的回答,我正在尝试您的方式。如果我能找到解决方案,我不会忘记接受你的回答并投票给你:)
    • 但是为什么要分别查询这三个属性呢?他们都来自同一张唱片。只需获取两个模型实例并将它们传递给模板即可。
    • 不是这个函数的完整代码。此函数将通过从数据库中获取值来计算标记。感谢您的建议。
    猜你喜欢
    • 2018-07-31
    • 2012-07-27
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2014-01-24
    相关资源
    最近更新 更多