简体   繁体   中英

Is there a better way to iterate over tuples in a (Django) template in Google App Engine?

Basically, what I'm trying to do is to render os.environ in a template in Google App Engine . I believe the technology is (or is adapted from) the Django template engine version 0.96 (but correct me if I'm wrong).

I found this question suggesting that you could do:

{{ for key, value in environ}}

But when I try that, I get an error saying:

'for' statements with five words should end in 'reversed': for key, value in environ

I guess that question was concerning another version of Django?

By the way, the value of environ is set to os.environ.items() before rendering the template.

Anyway, I came up a key_value_pair class that I could use instead:

class key_value_pair:
    def __init__(self, key, value):
        self.key = key
        self.value = value

def make_kvp(key, iter):
    return key_value_pair(key, iter[key])

make_kvp is small "factory" method that I later use to set the environ template value like this:

map(lambda x : make_kvp(x, os.environ), os.environ)

When doing that everything works just fine, but since I'm a totally new to the technologies in play here, I just wanted to make sure that I'm not overseeing some obvious simpler solution.

只需使用单个名称遍历序列,然后为该名称编制索引以获取各个元素。

Also, in template you should write like this:

{% for x in dic %}

{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM