如何在 Python 中将列表作为变量返回并在 Jinja2 中使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3970420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I return a list as a variable in Python and use in Jinja2?
提问by Handloomweaver
I am a very young programmer and I am trying to do something in Python but I'm stuck. I have a list of users in Couchdb (using python couchdb library & Flask framework) who have a username (which is the _id) and email. I want to use the list of email addresses in a select box in a jinja2 template.
我是一个非常年轻的程序员,我正在尝试用 Python 做一些事情,但我被卡住了。我在 Couchdb 中有一个用户列表(使用 python couchdb 库和 Flask 框架),他们有一个用户名(即 _id)和电子邮件。我想在 jinja2 模板的选择框中使用电子邮件地址列表。
My first problem is how to access the email addresses. If I do:
我的第一个问题是如何访问电子邮件地址。如果我做:
for user in db:
doc = db[user]
emails = doc['email']
print options
I get:
我得到:
[email protected]
[email protected]
[email protected]
So I can get my list of emails. But where my brutal inexperience is showing up is that I don't know how to then use them. The list only exists in the for loop. How do I return that list as a useable list of variables? And how do I then make that list appear in my jinja2 template in an option dropdown. I guess I need a function but I am a green programmer.
所以我可以得到我的电子邮件列表。但我残酷的经验不足之处在于我不知道如何使用它们。该列表仅存在于 for 循环中。如何将该列表作为可用的变量列表返回?以及如何使该列表出现在我的 jinja2 模板中的选项下拉列表中。我想我需要一个函数,但我是一个绿色程序员。
Would so appreciate help.
非常感谢帮助。
采纳答案by Josh
Assuming you have a model such as:
假设您有一个模型,例如:
class User(Document):
email = TextField()
You can use the static method loadof the User class
您可以使用loadUser 类的静态方法
users = [User.load(db, uid) for uid in db]
Now you can do this:
现在你可以这样做:
for user in users:
print user.id, user.email
But you're using it in flask so, in your view you can send this list of users to your template using something like this:
但是您在烧瓶中使用它,因此在您看来,您可以使用以下内容将此用户列表发送到您的模板:
from flask import render_template
@app.route("/users")
def show_users():
users = [User.load(db, uid) for uid in db]
return render_template('users.html', users=users)
Now in the users.htmljinja2 template the following will output a dropdown listbox of each user's e-mail
现在在users.htmljinja2 模板中,以下将输出每个用户电子邮件的下拉列表框
<select>
{% for user in users %}
<option value="{{ user.id }}">{{ user.email }}</option>
{% endfor %}
</select>
Also, are you using the Flask-CouchDB extension? It might be helpful in abstracting out some of the low level couchdb coding: http://packages.python.org/Flask-CouchDB/
另外,您是否使用 Flask-CouchDB 扩展?它可能有助于抽象出一些低级 couchdb 编码:http: //packages.python.org/Flask-CouchDB/
Disclaimer: The code above wasn't tested, but should work fine. I don't know much about CouchDB, but I am familiar with Flask. Also, I obviously didn't include a full Flask/CouchDB application here, so bits of code are missing.
免责声明:上面的代码没有经过测试,但应该可以正常工作。我对 CouchDB 了解不多,但对 Flask 比较熟悉。此外,我显然没有在这里包含完整的 Flask/CouchDB 应用程序,因此缺少一些代码。
回答by Paulo Scardine
lista = [ x for x in db ] # watch out for big databases, you can run out of memory
回答by nearlymonolith
You pass parameters to a jinja template as a dictionary dwhen you call the template.renderfunction(d)function (for example). Thus, you could do:
d当您调用template.renderfunction(d)函数时,您将参数作为字典传递给 jinja 模板(例如)。因此,你可以这样做:
emails = []
for user in db:
doc = db[user]
emails.append(doc['email'])
some_jinja_template.render({'list_of_emails' : emails})
Then in the template, you could do something like:
然后在模板中,您可以执行以下操作:
<ul>
{% for address in list_of_emails %}
<li><a href="mailto:{{ address }}">Send email to {{ address }}</a></li>
{% endfor %}
</ul>
To make a list of emails, for example, or handle them however you'd like.
例如,制作电子邮件列表,或根据需要处理它们。
PS - I'm sure the code could be more elegant/more optimized with a list comprehension or whatever, but I figured I should emphasize readability for a so-called "green" programmer.
PS - 我确信代码可以通过列表理解或其他方式更优雅/更优化,但我认为我应该强调所谓的“绿色”程序员的可读性。

