views:

50

answers:

1

I'm using Django templating with Google App Engine. I'm trying unsuccessfully to print out a menu.

The controller:

    menu_items = {'menu_items': [{'href': '/', 'name': 'Home'},
                                 {'href': '/cart', 'name': 'Cart'}],
                  }
    render('Views/menu.html', self, {'menu_items': menu_items})

# ...
def render(filename, main, template_values): 
    path = os.path.join(os.path.dirname(__file__), filename)
    main.response.out.write(template.render(path, template_values))

menu.html:

<ul>
    {% for page in menu_items %}
    <li><a href="{{page.href}}">{{page.name}}</a></li>
    {% endfor %}
</ul>

The HTML produced:

<li><a href=""></a></li>

What am I doing wrong here?

+5  A: 
menu_items = {'menu_items': [{'href': '/', 'name': 'Home'},
                             {'href': '/cart', 'name': 'Cart'}],
              }
render('Views/menu.html', self, {'menu_items': menu_items})

Look at these lines carefully. menu_items (dictionary) has a key menu_items with a value having a type list. And you're passing menu_items (dict) to render, so for page in menu_items actually refers to 'menu_items' (key).

Just change your code to look like:

menu_items = [{'href': '/', 'name': 'Home'}, {'href': '/cart', 'name': 'Cart' }]

and you're done...

Aviral Dasgupta