Nick Heiner
Nick Heiner

Reputation: 122392

Google App Engine: Basic Django Issue

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?

Upvotes: 0

Views: 135

Answers (1)

aviraldg
aviraldg

Reputation: 9154

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...

Upvotes: 5

Related Questions