开发者

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

开发者 https://www.devze.com 2023-01-21 09:51 出处:网络
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 i

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开发者_JAVA技巧 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.


Simply iterate over the sequence using a single name, then index the name to get at the individual elements.


Also, in template you should write like this:

{% for x in dic %}

{% endfor %}
0

精彩评论

暂无评论...
验证码 换一张
取 消