这里有什么问题?迭代Django模板中的字典 [英] What's wrong here? Iterating over a dictionary in Django template

查看:106
本文介绍了这里有什么问题?迭代Django模板中的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Django模板中迭代一个模型值的字典 - 我想列出每个模型字段的verbose_name及其值。



这里是我在models.py中有什么:

  class Manors(models.Model):
structidx = models.IntegerField primary_key = True,verbose_name =ID)
county = models.CharField(max_length = 5,null = True,blank = True,verbose_name =County)

def get_fields ):
d = {}
#d [database] =pubs
#d [uid] =sa
为Manors._meta中的字段。字段:
d [field.verbose_name(self)] = field.value_to_string(self)
return d

在views.py:

  manor_stats = Manors.objects.get(structidx__exact = id)
return render_to_response('template.html',{'place':place,'manor_stats':manor_stats},context_inst ance = RequestContext(request))

在模板中:

 < h4>统计信息< / h4> 
< ul>
{%for key,value in manor_stats.get_fields%}
< li> {{key}}:{{value}}< / li>
{%endfor%}
< / ul>

但是我只是得到一个奇怪的,扭曲的列表,如:

  u:i 
d:a

如果我在models.py中使用硬编码的值(如上图所示),甚至不起作用。



这里有什么问题?试着工作几个小时:(



---------- UPDATED -------------- -



尝试使用

  def get_fields(self):
d = {}
for Manors._meta.fields中的字段
d [field.verbose_name(self)] = {verbose:field.verbose_name(self),value:field.value_to_string (self)}
return d

和模板中:

 < h4>统计数据< / h4> 
< ul>
{%的键值,manor_stats.get_fields中的值%
< li> {{key}}:{{value}}< / li>
{%endfor%}
< / ul>

只是产生一个空白列表....

解决方案

要迭代字典不需要:

 < h4>统计信息< / h4> 
< ul>
{%for key,value in manor_stats.get_fields.items%}
< li> {{key}}:{{value}}< / li>
{%endfor%}
< / ul>

但是我建议先从函数中检索字典:



Views.py:

  manor_stats = Manors.objects.get(structidx__exact = id).get_fields )
return render_to_response('template.html',{'place':place,'manor_stats':manor_stats},context_instance = RequestContext(request))

然后:

 < h4>统计信息< / h4> 
< ul>
{%for key,value in manor_stats.items%}
< li> {{key}}:{{value}}< / li>
{%endfor%}
< / ul>

但只是因为我不太熟悉模板系统可以做多少取消引用。看到你知道如何尊重它,你正在节省让渲染器工作的努力。


I'm trying to iterate over a dictionary of model values in a Django template - I want to list the verbose_name of each model field alongside its value.

Here's what I have in models.py:

class Manors(models.Model):
    structidx = models.IntegerField(primary_key=True, verbose_name="ID")    
    county = models.CharField(max_length=5, null=True, blank=True, verbose_name="County")   

    def get_fields(self):
            d = {}
            #d["database"] = "pubs"
            #d["uid"] = "sa"
            for field in Manors._meta.fields:
                d[field.verbose_name(self)] = field.value_to_string(self)
            return d

And in views.py:

    manor_stats = Manors.objects.get(structidx__exact=id)
    return render_to_response('template.html', { 'place' : place, 'manor_stats' : manor_stats }, context_instance = RequestContext(request))

And in the template:

<h4>Statistics</h4>
<ul>
 {% for key, value in manor_stats.get_fields %}
 <li> {{ key }}: {{ value }} </li>
{% endfor %}
</ul>

But I just get a weird, distorted-looking list like:

u: i
d: a

It doesn't even work if I use hard-coded values in models.py (as shown commented out above).

What's wrong here? Been trying to work this out for hours:(

---------- UPDATED ---------------

Trying with

def get_fields(self):
        d = {}
        for field in Manors._meta.fields:
            d[field.verbose_name(self)] = { "verbose": field.verbose_name(self), "value": field.value_to_string(self) }
        return d

and in template:

<h4>Statistics</h4>
<ul>
 {% for key, value in manor_stats.get_fields %}
 <li> {{ key }}: {{ value }}</li>
{% endfor %}
</ul>

just produces a blank list....

解决方案

To iterate a dictionary wouldn't you need:

<h4>Statistics</h4>
<ul>
 {% for key, value in manor_stats.get_fields.items %}
 <li> {{ key }}: {{ value }}</li>
 {% endfor %}
</ul>

But I'd suggest retrieving the dictionary from the function first:

Views.py:

    manor_stats = Manors.objects.get(structidx__exact=id).get_fields()
    return render_to_response('template.html', { 'place' : place, 'manor_stats' : manor_stats }, context_instance = RequestContext(request))

And then:

<h4>Statistics</h4>
<ul>
 {% for key, value in manor_stats.items %}
 <li> {{ key }}: {{ value }}</li>
 {% endfor %}
</ul>

But only because I'm not that familiar with how much dereferencing the templating system can do. Seeing as you know how to deference it you're saving the effort of having the renderer work it out.

这篇关于这里有什么问题?迭代Django模板中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆