| 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 |  | 
          
            |  | 18 | 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. | 
          
            |  | 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 | {{{ | 
          
            |  | 23 | def 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 |  | 
          
            |  | 43 | A 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 | {{{ | 
          
            |  | 46 | def 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 |  | 
          
            |  | 53 | The 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 | {{{ | 
          
            |  | 56 | def 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 | }}} |