如何在Python中作为变量返回列表,在Jinja2中使用列表?

时间:2021-11-14 20:45:41

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@domain.com
otheremail@otherdomain.com
yetanotheremail@yetanotherdomain.com

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.

所以欣赏会有所帮助。

3 个解决方案

#1


16  

Assuming you have a model such as:

假设您有一个模型,例如:

class User(Document):
    email = TextField()

You can use the static method load of the User class

您可以使用User类的静态方法负载

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:

但是你在flask中使用它所以,在你的视图中你可以使用如下方法将用户列表发送到你的模板

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.html jinja2 template the following will output a dropdown listbox of each user's e-mail

现在的用户。html jinja2模板下面将输出每个用户的电子邮件的下拉列表框。

<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应用程序,所以缺少一些代码。

#2


3  

You pass parameters to a jinja template as a dictionary d when you call the template.renderfunction(d) function (for example). Thus, you could do:

当您调用template.renderfunction(d)函数时,您将参数传递给jinja模板作为字典d。因此,你可以:

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 -我相信代码可以更优雅/更优化与列表理解或其他,但我认为我应该强调可读性为所谓的“绿色”程序员。

#3


1  

lista = [ x for x in db ] # watch out for big databases, you can run out of memory

#1


16  

Assuming you have a model such as:

假设您有一个模型,例如:

class User(Document):
    email = TextField()

You can use the static method load of the User class

您可以使用User类的静态方法负载

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:

但是你在flask中使用它所以,在你的视图中你可以使用如下方法将用户列表发送到你的模板

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.html jinja2 template the following will output a dropdown listbox of each user's e-mail

现在的用户。html jinja2模板下面将输出每个用户的电子邮件的下拉列表框。

<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应用程序,所以缺少一些代码。

#2


3  

You pass parameters to a jinja template as a dictionary d when you call the template.renderfunction(d) function (for example). Thus, you could do:

当您调用template.renderfunction(d)函数时,您将参数传递给jinja模板作为字典d。因此,你可以:

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 -我相信代码可以更优雅/更优化与列表理解或其他,但我认为我应该强调可读性为所谓的“绿色”程序员。

#3


1  

lista = [ x for x in db ] # watch out for big databases, you can run out of memory