Changes between Version 1 and Version 2 of TemplatePitfalls


Ignore:
Timestamp:
Aug 20, 2005, 10:23:32 PM (19 years ago)
Author:
garthk
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TemplatePitfalls

    v1 v2  
    1 describe TemplatePitfalls here
     1Here's how to dodge some of the more obvious template pitfalls:
    22
    33
    4 
    5 ----
     4== Debugging ==
    65
    76Not a pitfall, but a tip:
     
    98Use {% debug %} in a template block if you want to know what is defined and can be used in a template.
    109
    11 Any tips on how to format the debug output nicer?
     10The debug output is pretty ugly unless you look at it through View Source.
    1211
    13 ----
     12== Anonymity ==
     13
     14{{{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.
     15
     16== Context variables and template inheritance ==
     17
     18To 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.
     19
     20{{{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:
     21
     22{{{
     23def site_vars(request, **kwargs):
     24    """Determine context variables for the site's base template."""
     25    user = request.user
     26    is_anonymous = user.is_anonymous()
     27    if is_anonymous:
     28        username = name = '(anonymous)'
     29    else:
     30        username = user.username
     31        name = user.get_full_name()
     32        if not name:
     33            name = username
     34    vars = {
     35        'is_anonymous': is_anonymous,
     36        'name': name,
     37        'username': username,
     38    }
     39    vars.update(kwargs)
     40    return vars
     41}}}
     42
     43A custom version of {{{app_vars}}} goes in each {{{sitename/apps/appname/views/appname.py}}} (or, if you're flattening the namespace, {{{sitename/apps/appname/views.py}}}):
     44
     45{{{
     46def app_vars(request, **kwargs):
     47    """Determine context variables for the application and site base templates."""
     48    vars = {} # set your app variables here
     49    vars.update(site_vars(request, **kwargs))
     50    return vars
     51}}}
     52
     53The keyword arguments let you define additional useful variables for your page without having to further manipulate the dictionary: just keep piling on the arguments to {{{app_vars}}}. Let's say your base template likes to have a {{{title}}} variable available to it:
     54
     55{{{
     56def index(request):
     57    """View the appname index."""
     58
     59    # Customise the app variables the hard way
     60    vars = app_vars(request)
     61    vars['title'] = "Index"
     62
     63    # Or, the easy way
     64    vars = app_vars(request, title="Index")
     65
     66    # Load the template, construct the context, and render one against the other:
     67    t = template_loader.get_template('appname_index')
     68    c = Context(request, vars)
     69    return HttpResponse(t.render(c))
     70}}}
Back to Top