2

I have a base_template.html that uses a context variable my_context_var. All of my other templates extends this base template. Can you help me answer one of these questions? (They are just different ways to look at the same underlying problem)

  1. How can I make my_context_var available to all the derived templates but allow a way to override my_context_var for specific views.

  2. How can I inject my_context_var into the template context only if that variable is not already there.

Essentially, I'm looking for a setdefaults() functionality for context variables. If it helps, my_context_var is basically a blank search form that is available in most pages of my site. I need to override it for one page that displays both the form and the result. The current problem is that I keep getting the blank form even for that result page.

lastoneisbearfood
  • 3,955
  • 5
  • 26
  • 25

3 Answers3

1

You have a couple options:

You can create your own RequestContext and include all the variables you want through your site: Documentation

Or you can create template tags and access them in any template: Documentation

lastoneisbearfood
  • 3,955
  • 5
  • 26
  • 25
D.A
  • 2,555
  • 1
  • 14
  • 10
0

Since it should apply for all of your views the best option is to create a custom context processor. Templatetags would have the disadvantage that you have to include them everywhere you need this variable. Creating your own RequestContext as D.A. stated is overkill in my opinion.

Torsten Engelbrecht
  • 13,318
  • 4
  • 46
  • 48
  • I tried the context processor route but it seems there isn't a way to inspect the current context and check for the existence of ```my_context_var``` – lastoneisbearfood Aug 10 '12 at 09:44
  • Can you post the code of your context processor definition? Its hard to get what exactly fails without it. – Torsten Engelbrecht Aug 10 '12 at 09:49
  • Sorry, I meant create a context processor, but be sure to use a RequestContext. My words get crossed sometimes. – D.A Aug 10 '12 at 12:42
  • Yes, I understood. Just wanted to see the implementation of your context processor. – Torsten Engelbrecht Aug 11 '12 at 02:58
  • the context processor is really simple: – lastoneisbearfood Aug 12 '12 at 09:08
  • the context processor is really simple: ```def dc_simple_search(request): return {'simple_search_form': SimpleSearchForm()}``` For most of my pages, this is fine. But for the page that is the form action, I need to render ```{{simple_search_form.fields}``` and it doesn't seem possible because the context processor's version of ```simple_search_form``` always overwrite the view function's version since context_processor is used to update the context after the view function has finished execution – lastoneisbearfood Aug 12 '12 at 09:14
  • Ok, now I got your point. I see the problem. A workaround is though to add a variable to the `request` object (like they do with django auth for example, e.g. `request.user`). This one could either indicate if the form is already there or need to be added. Though it is kind of a hacky solution. – Torsten Engelbrecht Aug 13 '12 at 01:31
0

If you want to set the variable within a template (but only if it doesn't already exist), the following should work:

{% with my_context_var=my_context_var|default:"Value you want to set it to" %}
    {# The variable is accessible within then with block #}
    {{ my_context_var }}
{% endwith %}
mhost
  • 6,930
  • 5
  • 38
  • 45
  • This doesn't set `my_context_var` only if it isn't already in the context, it sets it if it's not `True`, which is not the same thing. If `my_context_var` were set to an empty list, for instance, you'd get the default value. – Duncan Parkes Sep 10 '13 at 15:20