Changes between Version 4 and Version 5 of TemplatePitfalls
- Timestamp:
- Aug 30, 2005, 3:52:13 PM (19 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TemplatePitfalls
v4 v5 1 Here's how to dodge some of the more obvious template pitfalls: 1 = Template pitfalls = 2 2 3 Here's how to dodge some of the more obvious template pitfalls: 3 4 4 5 == Debugging == … … 6 7 Not a pitfall, but a tip: 7 8 8 Use { % debug %} in a template block if you want to know what is defined and can be used in a template.9 Use {{{{% debug %}}}} in a template block if you want to know what is defined and can be used in a template. 9 10 10 11 The debug output is pretty ugly unless you look at it through View Source, or you can do it this way: … … 24 25 }}} 25 26 26 == Anonymity ==27 28 {{{user.is_anonymous}}} is a method, not an attribute, which makes it hard to test against in a template. Set your own context variable {{{is_anonymous}}}, instead.29 30 27 == Context variables and template inheritance == 31 28 32 To solve ''Anonymity'' above, it's handy to define a method each view can call to fill in its {{{Context}}} with the right variables. If you're inheriting templates, you should also do the same with the method. I've ended up with an {{{app_vars}}} method in each app's views module which calls {{{site_vars}}} in the site.29 It's handy to define a method each view can call to fill in its {{{Context}}} with the right variables. If you're inheriting templates, you should also do the same with the method. I've ended up with an {{{app_vars}}} method in each app's views module which calls {{{site_vars}}} in the site. 33 30 34 31 {{{site_vars}}} goes in {{{sitename/views/sitename.py}}} if you're trying to match the default shape of the application namespace, or in {{{sitename/views.py}}} if you're flattening it: