Changes between Version 5 and Version 6 of TemplatePitfalls
- Timestamp:
- Sep 6, 2005, 7:23:52 AM (19 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TemplatePitfalls
v5 v6 25 25 }}} 26 26 27 == Context variables and template inheritance==27 == custom context variables for generic views == 28 28 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.29 More advanced versions of the base template (mine is {{{base.html}}}, following the model set in the administration interface) might need a consistent set of context variables generated on the fly based on the identity of the current user, what they're doing, what's happening elsewhere on the site, and so on. 30 30 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:31 If you're using generic views, though, there's no obvious way to make these context variables available. 32 32 33 {{{ 34 def site_vars(request, **kwargs): 35 """Determine context variables for the site's base template.""" 36 user = request.user 37 is_anonymous = user.is_anonymous() 38 if is_anonymous: 39 username = name = '(anonymous)' 40 else: 41 username = user.username 42 name = user.get_full_name() 43 if not name: 44 name = username 45 vars = { 46 'is_anonymous': is_anonymous, 47 'name': name, 48 'username': username, 49 } 50 vars.update(kwargs) 51 return vars 52 }}} 33 The solution is to use [http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-tags custom template tags], which can modify a page's context variables on the fly. 53 34 54 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}}}): 35 If your needs are middling simple, or you don't want to have to write your own custom template tag from scratch, I've developed a generic option: [http://www.deadlybloodyserious.com/categories/django/2005/09/06.html vars], which defines a fairly generic {{{pushvars}}} tag that calls the function of your choice and pushes its results into the current context. 55 36 56 {{{ 57 def app_vars(request, **kwargs): 58 """Determine context variables for the application and site base templates.""" 59 vars = {} # set your app variables here 60 vars.update(site_vars(request, **kwargs)) 61 return vars 62 }}} 37 I'm posting this to atone for my previous crummy advice on this Wiki page. 63 38 64 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: 65 66 {{{ 67 def index(request): 68 """View the appname index.""" 69 70 # Customise the app variables the hard way 71 vars = app_vars(request) 72 vars['title'] = "Index" 73 74 # Or, the easy way 75 vars = app_vars(request, title="Index") 76 77 # Load the template, construct the context, and render one against the other: 78 t = template_loader.get_template('appname_index') 79 c = Context(request, vars) 80 return HttpResponse(t.render(c)) 81 }}} 39 {{{- garthk}}}