1

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.

Community
  • 1
  • 1
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222

2 Answers2

1

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • With index you mean like `kvp[0]`? Because I tried that and its not working. But I guess my syntax is incorrect. I'm getting a `Could not parse the remainder: [0]` error. – Klaus Byskov Pedersen Oct 20 '10 at 20:45
  • 1
    You use periods in Django templates to index sequences. `kvp.0` – Ignacio Vazquez-Abrams Oct 20 '10 at 20:47
  • @klausbyskov: Use `.` in your template. `kvp.0`. The template language is not Python. You might want to look at more of the Django tutorial. – S.Lott Oct 20 '10 at 20:48
  • Thank you very much to both of you. @S.Lott you are completely correct, I *do* have to read a lot more of the Django docs but I have been reading so many (other) docs today and I just wanted to get my feet wet with some coding. And I got stuck. Silly me, but I'm sure you know the feeling. – Klaus Byskov Pedersen Oct 20 '10 at 20:53
  • "I'm sure you know the feeling". Sadly, no, I don't know that feeling. Sorry. – S.Lott Oct 20 '10 at 20:56
0

Also, in template you should write like this:

{% for x in dic %}

{% endfor %}
Anthon
  • 69,918
  • 32
  • 186
  • 246