How do I return a list as a variable in Python and use in Jinja2?

前端 未结 3 1660
盖世英雄少女心
盖世英雄少女心 2021-02-05 18:45

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

3条回答
  •  误落风尘
    2021-02-05 19:12

    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:

    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:

    
    

    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.

提交回复
热议问题