tags:

views:

68

answers:

1

Given

siteInfo = \
{
    'appname3': 'MSQuantDynamics11',
    'siteBase': 'http://www.pil.sdu.dk/1',
}

in a "urls.py" file.

This works as expected:

urlpatterns = patterns('',
    (r'^$',  direct_to_template,                         \
      {                                                  \
        'template'     : "homepage.html",                \
        'extra_context': { 'siteInfo': siteInfo },       \
      }
    ),
)

Why does it not work with the following? (the result of "{{ siteInfo.appname3 }}" in homepage.html becomes emtpy):

urlpatterns = patterns('',
    (r'^$',  direct_to_template,                         \
      {                                                  \
        'template'     : "homepage.html",                \
        'extra_context': siteInfo,                       \  
      }
    ),
)

Would it work if "siteInfo.appname3" was changed to something else?

+4  A: 

It should work if instead of {{siteInfo.appname3}} you just use {{ appname3 }}.

extra_context is expected to be a dict, and siteInfo is a dict, so you can just pass it straight to extra_context, but the key-value pairs will then be directly accessible in the template, rather than accessible through {{ siteInfo.key }}.

In the first example, you're creating a dict to be passed into extra_context, with the key siteInfo, and the value being the dict siteInfo. In the second, you're passing the dict siteInfo directly.

Dominic Rodger