Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django模板中迭代字典时卡住_Python_Html_Django - Fatal编程技术网

Python 在django模板中迭代字典时卡住

Python 在django模板中迭代字典时卡住,python,html,django,Python,Html,Django,我有这本字典:- a = {'title': ['Turning a MacBook into a Touchscreen with $1 of Hardware (2018)', 'Supercentenarians are concentrated into regions with no birth certificates', 'Open list of GDPR fines so far'], 'url': ['https://www.anishathalye.com/2018/04/

我有这本字典:-

a = {'title': ['Turning a MacBook into a Touchscreen with $1 of Hardware (2018)', 'Supercentenarians are concentrated into regions with no birth certificates', 'Open list of GDPR fines so far'], 'url': ['https://www.anishathalye.com/2018/04/03/macbook-touchscreen/', 'https://www.biorxiv.org/content/10.1101/704080v1', 'https://github.com/lknik/gdpr/blob/master/fines/README.md'], 'type': ['story', 'story', 'story'], 'n': [0, 1, 2]}
我将其传递到模板中。 现在,我想得到的是第一个“title”和“type”,以及它的“url”。 我尝试了以下代码:-

<ul>
{% for i in n %}
<br/>
{{i}}
<li>{{title.i}}</li><br/>
<li>{{url.i}}</li>
{%endfor%}
    {n%中的i的%n}
    {{i}
  • {{title.i}

  • {{url.i}
  • {%endfor%}
但无法获得所需的输出。 我期望的输出是:-
标题:用1美元的硬件将MacBook变成触摸屏(2018)
URL
类型:故事 以这种方式,标题的连续列表后跟url和类型。
我得到的输出是一个空白屏幕。

请帮帮我。

像这样的东西怎么样:

<ul>
  {% for key, values in a.items %}
    {% if key != 'n' %}
      <br/>
      <li>{{ key }}: {{ values.0}}</li>
      {% endif %}
    {% endfor %}
</ul>
    {%表示键,值在a.items%} {%if键!=“n”}
  • {{key}}:{{values.0}
  • {%endif%} {%endfor%}

不过,我不推荐您使用这样的上下文,
但是如果你不想改变它,你可以使用CustomSimple

然后在模板中(别忘了先加载它)


我建议这样做,如果在视图中使用,它会更简单

title = ['Turning a MacBook into a Touchscreen with $1 of Hardware (2018)', 'Supercentenarians are concentrated into regions with no birth certificates', 'Open list of GDPR fines so far']

url = ['https://www.anishathalye.com/2018/04/03/macbook-touchscreen/', 'https://www.biorxiv.org/content/10.1101/704080v1', 'https://github.com/lknik/gdpr/blob/master/fines/README.md']

type = ['story', 'story', 'story']

zipped = zip(title, url, type)
a = {'zipped': zipped}
然后在模板中

{% for title, url, type in zipped %}
Title: {{ title }}
URL: {{ url }}
Type: {{ type }}
{% endfor %}

或者用另一种方式,使用字典列表代替

my list = [
    {'title': 'Turning a MacBook into a Touchscreen with $1 of Hardware (2018)', 'url': 'https://www.anishathalye.com/2018/04/03/macbook-touchscreen/', 'type':'story'},
    {'title': '...', 'url': '...', 'type': 'story'}, 
    {'title': '...', 'url': '...', 'type': 'story'}
]
a = {'my_list': my_list}
然后用以下方法进行迭代:

{% for item in my_list %}
Title: {{ item.title}}
URL: {{ item.url}}
Type: {{ item.type }}
{% endfor %}

要将此目录列表更改为目录列表对您来说不是更容易吗?->
a=[{'title':'blablabla','url':'www…,'type':'story'},{'title'…'},{}]
是否从视图向模板发送上下文?上下文是'a'@HemanthSPNot Working!:(如何将dict传递给模板?在views.py:-返回HttpResponse(template.render(a,request)),其中a是上面提到的dict。好的,它应该是
a.items
,然后,我将更新答案。我也尝试了a.items,但没有工作。没有显示任何结果。)
my list = [
    {'title': 'Turning a MacBook into a Touchscreen with $1 of Hardware (2018)', 'url': 'https://www.anishathalye.com/2018/04/03/macbook-touchscreen/', 'type':'story'},
    {'title': '...', 'url': '...', 'type': 'story'}, 
    {'title': '...', 'url': '...', 'type': 'story'}
]
a = {'my_list': my_list}
{% for item in my_list %}
Title: {{ item.title}}
URL: {{ item.url}}
Type: {{ item.type }}
{% endfor %}