Re: [flask] How do I return a list as a variable in Python and use
in Jinja2?
- From:
- Dag Odenhall
- Date:
- 2010-10-19 @ 17:54
On Tue, 2010-10-19 at 17:21 +0100, Alasdair Macmillan wrote:
> I am a real beginner 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.
>
> 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.
>
> Would so appreciate help.
>
You can make a list like so:
emails = [db[user]['email'] for user in db]
Or depending on if the couchdb lib mimics a dict:
emails = [user['email'] for user in db.itervalues()]
This syntax is documented in the tutorial:
http://docs.python.org/tutorial/datastructures.html#list-comprehensions
But you can do a for loop in Jinja anyway, something like:
<ul>
{% for user in db %}
<li>{{ db[user].email }}</li>
{% endfor %}
</ul>
Also you might want to have a look at one of the Flask extensions for
CouchDB:
* http://packages.python.org/Flask-CouchDB/ ("approved" by the Flask
developer)
* http://packages.python.org/Flask-CouchDBKit/
Jinja is documented here: http://jinja.pocoo.org/
Re: [flask] How do I return a list as a variable in Python and use in Jinja2?
- From:
- danjac354@gmail.com
- Date:
- 2010-10-19 @ 16:56
Hi Alasdair,
I think we need a bit more context. What does your view look like ?
On 19 October 2010 17:21, Alasdair Macmillan <al@atomised.coop> wrote:
> I am a real beginner 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.
>
> 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.
>
> Would so appreciate help.
>
>