从字典中调用值(Django)

时间:2011-12-20 23:15:02

标签: django dictionary

我是Django的新手,我无法从models.py中生成的字典中将值加载到HTML中,如下所示:

>>> generic_id = Generic.objects.get(pk=127)
>>> dict = generic_id._get_dict()
>>> dict  
[{'field__name':'key1', 'value_char':'value1a', 'value_num':'value1'},{'field__name':'key2', 'value_char':'value2a', 'value_num':'value2'},{'field__name':'key3', 'value_char':'value3a', 'value_num':'value3'},{'field__name':'key4', 'value_char':'value4a', 'value_num':'value4'}]
>>> dict[2]['value_num']  
Decimal('value2')
>>> dict[3]['value_char']
'value3a'

HTML表格如下所示:

<table>
  <tr>
    <td>Description1</td><td>{{value1}}</td>
    <td>Description2</td><td>{{value2}}</td>
    <td>Description3</td><td>{{value3a}}</td>
    <td>Description4</td><td>{{value4}}</td>
  </tr>
  <tr>
    <td>Name: {{ generic.name }}</td>
    <td>E-mail: {{ generic.email }}</td>
    <td>State: {{ generic.state }}
</table>

现在views.py中的代码如下所示:

def query_test(request, generic_id):
    try:
        a = Generic_table.objects.get(pk=generic_id)
    except Generic_table.DoesNotExist:
        raise Http404
    t = loader.get_template('query_test.html')
    c = RequestContext(request, {
             'generic' : a, })
    return HttpResponse(t.render(c))

有人可以就如何(并且有效地)从字典中获取适当的值到生成的HTML中给出一些建议吗?

1 个答案:

答案 0 :(得分:1)

根据您的模型和模板中的对象,我建议您尝试这样做:

假设:

a = Generic.objects.get(pk=generic_id)
# a == {'field__name':'key1', 'value_char':'value1a', 'value_num':'value1'}

views.py

from django.shortcuts import render_to_response, get_object_or_404

def query_test(request, generic_id):
    a = get_object_or_404(Generic, pk=generic_id)

    return render_to_response("query_test.html", a, 
                    context_instance=RequestContext(request))

query_test.html

Name: {{field__name}}
Char: {{value_char}}
Num : {{value_num}}

您的视图未显示您期望多个对象,因为您查找了一个ID,因此您的模板最终只会格式化一个对象。

修改:如果您要显示结果列表

views.py看起来像这样:

def query_test(request, generic_id=None):
    if generic_id is not None:
        a = Generic.objects.filter(pk=generic_id)
    else:
        a = Generic.objects.all()

    c = {'generic': a}

    # add some extra values
    c['value1'] = "I am value1"

    # add a whole dictionary of other values
    c.update({'value2': "yay val2", 'value3a': 3, 'value4': "I am 4"})

    return render_to_response("query_test.html", c, 
                    context_instance=RequestContext(request)) 

您的模板类似于:

<table>
<tr>
    <td>Description1</td><td>{{value1}}</td>
    <td>Description2</td><td>{{value2}}</td>
    <td>Description3</td><td>{{value3a}}</td>
    <td>Description4</td><td>{{value4}}</td>
</tr>
{% for item in generic %}
    <tr>
        <td>Name: {{item.field__name}}</td>
        <td>Char: {{item.value_char}}</td>
        <td>Num : {{item.value_num}}</td>
    </tr>
{% endfor %}
</table>

Edit2 :解决您要发送到模板的奇怪对象

根据您更新的问题...这不是字典。它是一个字典列表,你从单个模型实例中提取数据的方式真的很奇怪。但假设这是你真正想要的,你有很多选择..

1)在将数据对象发送到模板之前修复该数据对象。我不知道你是否想要该列表中的所有元素,或者只是一个特定的项目。

not_a_generic_id = Generic.objects.get(pk=127)
not_a_dict = not_a_generic_id._get_dict()
dict_that_I_actually_want = not_a_dict[1]
return render_to_response("query_test.html", dict_that_I_actually_want)

2)将整个列表发送到模板并循环遍历每个项目,然后访问值:

views.py

not_a_generic_id = Generic.objects.get(pk=127)
not_a_dict = not_a_generic_id._get_dict()
c = {"still_not_a_dict": not_a_dict}
return render_to_response("query_test.html", c)

template.html

<table>
{% for actual_dict in still_not_a_dict %}
    <tr>
        <td>Name: {{actual_dict.field__name}}</td>
        <td>Char: {{actual_dict.value_char}}</td>
        <td>Num : {{actual_dict.value_num}}</td>
    </tr>
{% endfor %}
</table>

3)即使模板不允许您实际访问列表的数字索引,因为您希望自己在视图中对数据进行排序...如果您坚持在模板中访问该列表的特定索引,与views.py的#2相同,但是:

template.html

{% for actual_dict in still_not_a_dict %}
    {% if forloop.counter == 1 %}
        {{actual_dict.value_char}}
    {% endif %}
{% endfor %}