Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty string in HTML rendered result from Django/Python

Tags:

django

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?

like image 416
Peter Mortensen Avatar asked Jun 19 '09 09:06

Peter Mortensen


1 Answers

Use {{ appname3 }} instead of {{siteInfo.appname3}} .

Because key-value pairs {{appname3}} can 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.

like image 184
Dominic Rodger Avatar answered Nov 08 '22 22:11

Dominic Rodger