在django中遍历多维词典 [英] Traversing multi-dimensional dictionary in django

查看:119
本文介绍了在django中遍历多维词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python地的第一天就是一个PHP人,试图将一个php网站转换成python(学习经历),而且我很伤心。我从来没有想过像pythoners这样调用它们会很难使用多维数组或字典。



所以我可以使用这个,但我可以不要在django模板中循环。 这个没有工作,但我想我不能循环通过它,如果我能让它上班。

  {%for key,val in dictionary.items%} 

只适用于实际的字典似乎,而不是托管多维字典类。



我正在从一个sql查询中创建我的字典:

  vid [video [7]] ['cat_short_name'] = video [2] 
vid [video [7]] ['cat_name'] = video [1]
vid [video [7]] [ 'cat_id'] = video [7]

vid [video [7]] ['companies'] [video [14]] ['comp_short_name'] = video [5]
vid [video [7]] ['companies'] [video [14]] ['comp_name'] = video [4]
vid [video [7]] ['companies'] [video [14]] [ 'comp_website'] = video [6]

vid [video [7]] ['companies'] [video [14]] ['video'] [video [8]] ['top_video ] = 0
vid [video [7]] ['companies'] [video [14]] ['video'] [video [8]] ['vid_id'] = video [8]
vid [video [7]] ['companies'] [video [14]] ['video'] [video [8]] ['vid_name'] = video [9]
vid [video [7]] ['公司] [视频[14]] ['视频] [视频[8]] ['vid_url'] =视频[1 0]

我基本上需要让所有公司在某个类别中,然后获取该公司的所有视频所以我可以很容易地嵌套在我的模板中。这是我如何在PHP中创建一个巨大的深阵列。尝试在Python中复制已被证明是困难的。



我想也许我可以使用set_MODEL在django中进行向后查找,但是我无法确定。 p>

对于如何完成我的目标的任何帮助将不胜感激。我希望我的问题清楚



编辑:



当我完成在我的模板中循环,它看起来像这样。 ..

 < h1>类别< / h1> 
< h2>公司< / h2>
< ul>
< li> video< / li>
< / ul>
< h2>公司< / h2>
< ul>
< li> video< / li>
< li> video< / li>
< / ul>
< h1>类别< / h1>
< h2>公司< / h2>
< ul>
< li> video< / li>
< / ul>
< h2>公司< / h2>
< ul>
< li> video< / li>
< li> video< / li>
< / ul>


解决方案

您应该使用内置的 ORM ,而不是使用您自己的查询(至少对于像这样简单的东西),使事情更容易(假设您还在models.py文件中构建了您的模型)



在您的视图中:

  def categories_view(request):
categories = Categories.objects.all()#maybe将order_by或过滤器放在这里
返回render_to_response(your_template.html ,{'categories':categories})

在您的模板中:

  {%类别中的类别%} 
< h1> {{category.name}}< / h1>
{%for category.company_set.all%}
< h2> {{company.name}}< / h2>
< ul>
{%for public.video_set.all%}
< li> {{video.name}}< / li>
{%endfor%}
< / ul>
{%endfor%}
{%endfor%}

它测试了它,但它应该工作。将这段代码与您在PHP或Python中不使用ORM时必须编写的代码进行比较。



查看django文档以了解更多信息,我建议您花几个小时,并执行教程



更新:修改代码以使用_set.all


I'm a PHP guy on my first day in Python-land, trying to convert a php site to python (learning experience), and I'm hurting for advice. I never thought it would be so hard to use multi-dimensional arrays or dictionaries as you pythoners call them.

So I can create multi-dimensional arrays using this, but i can't loop it in a django template. this doesnt work but i imagine i cant loop through it if i could get it to work.

{% for key,val in dictionary.items %}

only works for actual dictionaries it seems, not the custon multi-dimensional dictionary classes.

I'm creating my dictionary from a sql query:

vid[ video[ 7 ] ][ 'cat_short_name' ] = video[ 2 ]
vid[ video[ 7 ] ][ 'cat_name' ] = video[ 1 ]
vid[ video[ 7 ] ][ 'cat_id' ] = video[ 7 ]

vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_short_name' ] = video[ 5 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_name' ] = video[ 4 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'comp_website' ] = video[ 6 ]

vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'top_video' ] = 0
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_id' ] = video[ 8 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_name' ] = video[ 9 ]
vid[ video[ 7 ] ][ 'companies' ][ video[ 14 ] ][ 'videos' ][ video[ 8 ] ][ 'vid_url' ] = video[ 10 ]

I basically need to get all companies in a certain category and then get all videos in that company so i can nest them easily in my template. This is how i did it in php, creating one huge deep array. Trying to duplicate in Python has proven difficult.

I thought maybe i could do with the backwards lookups in django using set_MODEL but i couldn't figure that out either.

Any help on how to accomplish my goal would be appreciated. I hope my question is clear

EDIT:

When im done looping in my template it looks like this...

<h1>Category</h1>
   <h2>Company</h2>
   <ul>
        <li>video</li>
    </ul>
    <h2>Company</h2>
    <ul>
        <li>video</li>
        <li>video</li>
    </ul>
<h1>Category</h1>
   <h2>Company</h2>
   <ul>
        <li>video</li>
    </ul>
    <h2>Company</h2>
    <ul>
        <li>video</li>
        <li>video</li>
    </ul>

解决方案

You should be using the built in ORM instead of using your own queries (at least for something simple like this), makes things much easier (assuming you've also built your models in your models.py file)

In your view:

def categories_view(request):
    categories = Categories.objects.all()   #maybe put an order_by or filter here
    return render_to_response("your_template.html", {'categories':categories})

In your template:

{% for category in categories %}
    <h1>{{ category.name }}</h1>
    {% for company in category.company_set.all %}
        <h2>{{ company.name }}</h2>
        <ul>
        {% for video in company.video_set.all %}
            <li>{{ video.name }}</li>
        {% endfor %}
        </ul>
    {% endfor %}
{% endfor %}

I haven't tested it but it should work. Compare this code to what you would have to write if you weren't using ORM, in either PHP or Python.

take a look at the django docs for more info, I'd recommend taking a few hours and doing the tutorial.

Update: modified the code to use "_set.all"

这篇关于在django中遍历多维词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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