在Jinja2/Werkzeug中渲染python字典 [英] Rendering a python dict in Jinja2 / Werkzeug
问题描述
我正在使用URL缩短器(基于Werkzeug的Shortly演示应用程序为基础).
I'm playing with a url shortener (basing it on the Shortly demo app from Werkzeug).
我有这样的字典-
('1', {'target': 'http://10.58.48.103:5000/', 'clicks': '1'})
('3', {'target': 'http://slash.org', 'clicks': '4'})
('2', {'target': 'http://10.58.48.58:5000/', 'clicks': '1'})
('5', {'target': 'http://de.com/a', 'clicks': '0'})
在url_list中返回并由render_template使用
which is returned in url_list and used by render_template
def on_list_urls(self, request):
url_list = self.get_urls()
return self.render_template('list_urls.html',
url_list = url_list
)
模板list_urls非常简单-
the template list_urls is pretty simple -
{% extends "layout.html" %}
{% block title %}List URLs{% endblock %}
{% block body %}
<h2>List URLs</h2>
<ul id="items">
{% for item in url_list %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endblock %}
事实是,我似乎无法访问字典中的项目.
Thing is, I can't seem to access the items in the dict.
行
<li>{{ item }}</li>
是我关注的重点.如上所述,我获得了字典中键的列表.
is where I'm focussing attention. As above, I get a list of the keys in the dict.
<li>{{ item["target"] }}</li>
不返回任何内容.没有任何一个 {{user.url}}> {{user.username}} 在文档中输入内容似乎可行.
returns nothing. None of the {{ user.url }}">{{ user.username }} type stuff in the docs seems to work.
好吗?新手-温柔.谢谢.
Ideas please? Newbie - be gentle. Thanks.
更新
感谢您的答复.
Ewan的答案有效,但使用了一系列词典.我想传递一个字典并将其呈现(因为我想要一个非整数的项目索引). Jinja会这样做吗?
Ewan's answer works, but uses a list of dicts. I want to pass a dict and render that (because I want a non-integer index of items). Does Jinja do that?
也-我误输入了url_list.更像这样-
Also - I mis-represented url_list. It's more like this -
{'a': {'target': 'http://testing.com/test', 'clicks': '0'},
'1': {'target': 'http://10.58.48.103:5000/', 'clicks': '1'},
'3': {'target': 'http://slash.org', 'clicks': '4'},
'2': {'target': 'http://10.58.48.58:5000/', 'clicks': '1'}}
进一步的实验-传递字典会产生有关列表对象的错误.
Further experimentation - passing a dict produces an error about a list object.
{% for key in url_list.iteritems() %}
UndefinedError:列表对象"没有属性"iteritems"
UndefinedError: 'list object' has no attribute 'iteritems'
再次感谢.
仍然困惑为什么它认为我正在通过一个列表,但是现在让它可以正常工作.
Still baffled by why it thought I was passing a list but got it working now.
{% for key, value in url_list.iteritems() %}
<li>{{ key }} - {{ value["target"] }} - {{ value["clicks"] }}</li>
打印出所有内容.非常感谢.
prints out everything. Many thanks.
推荐答案
您的url_list
应该看起来像这样:
Your url_list
should look like this:
url_list = [{'target': 'http://10.58.48.103:5000/', 'clicks': '1'},
{'target': 'http://slash.org', 'clicks': '4'},
{'target': 'http://10.58.48.58:5000/', 'clicks': '1'},
{'target': 'http://de.com/a', 'clicks': '0'}]
然后使用:
<li>{{ item["target"] }}</li>
在您的模板中可以使用.
in your template will work.
您的模板认为您正在传递列表,那么您确定传递的是您的原始字典而不是我上面的列表吗?
Your template think you're passing a list in, so are you sure you're passing in your original dict and not my above list?
此外,您还需要在字典中同时访问key
和value
(当您传递字典而不是列表时):
Also you need to access both a key
and a value
in your dictionary (when you're passing a dictionary rather than a list):
Python 2.7
{% for key, value in url_list.iteritems() %}
<li>{{ value["target"] }}</li>
{% endfor %}
Python 3
{% for key, value in url_list.items() %}
<li>{{ value["target"] }}</li>
{% endfor %}
这篇关于在Jinja2/Werkzeug中渲染python字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!