89

I would like to know how to pass a variable to all my templates, without repeating the same code on every method in my views.py file?

In the example below I would like to make categories (an array of category objects) available to all templates in the web app.

Eg: I would like to avoid writing 'categories':categories on every method. Is it possible?

One view method

def front_page(request):
    categories = Category.objects.all()
    if is_logged_in(request) is False:
        return render_to_response('users/signup.html', {'is_logged_in': is_logged_in(request), 'categories':categories}, context_instance=RequestContext(request))
    else:
        return render_to_response('users/front_page.html', {'is_logged_in': is_logged_in(request), 'categories':categories},context_instance=RequestContext(request))

Another view method

def another_view_method(request):
    categories = Category.objects.all()
    return render_to_response('eg/front_page.html', {'is_logged_in': is_logged_in(request), 'categories':categories},context_instance=RequestContext(request))
ipegasus
  • 13,862
  • 8
  • 51
  • 72

1 Answers1

144

What you want is a context processor, and it's very easy to create one. Assuming you have an app named custom_app, follow the next steps:

  • Add custom_app to INSTALLED_APPS in settings.py (you've done it already, right?);
  • Create a context_processors.py into custom_app folder;
  • Add the following code to that new file:

    def categories_processor(request):
     categories = Category.objects.all()            
     return {'categories': categories}
    
  • Add context_processors.py to TEMPLATE_CONTEXT_PROCESSORS in settings.py

    TEMPLATE_CONTEXT_PROCESSORS += ("custom_app.context_processors.categories_processor", )
    

And now you can use {{categories}} in all the templates :D

As of Django 1.8

To add a TEMPLATE_CONTEXT_PROCESSORS, in the settings you must add the next code:

TEMPLATES[0]['OPTIONS']['context_processors'].append("custom_app.context_processors.categories_processor")

Or include that string directly in the OPTIONS.context_processors key in your TEMPLATES setting.

igorsantos07
  • 4,149
  • 4
  • 40
  • 59
Victor Castillo Torres
  • 10,061
  • 6
  • 37
  • 46
  • 3
    Is it possible to make this work for any app in the project, instead of just `custom_app`? – JacobF Feb 25 '14 at 10:00
  • 1
    @jvannistelrooy Yes, it's possible. – Victor Castillo Torres Feb 26 '14 at 01:18
  • This is so much easier to understand than other docs available elsewhere. Thanks. One thing: += doesn't work for me in D1.6. Instead: TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (...) – aljabear May 11 '14 at 01:13
  • Does this also work for dynamic type queries such as ones that are dependent on which user is logged in? For example, what if I wanted notifications to be a global variable but only for the user that is logged in? I shouldn't do something like querying every single person's notification right? Or do I just do notifications.objects.all() – EazyC Aug 04 '15 at 04:36
  • 1
    @EazyC As you can see the parameter of the function is a request, so you can get the logged user with request.user, and do whatever you want with it. – Victor Castillo Torres Aug 04 '15 at 04:41
  • @EazyC It's a pleasure :D – Victor Castillo Torres Aug 04 '15 at 19:59
  • Should the answer change with the newest Django? (bringing this up on Oct 2015) – tscizzle Oct 10 '15 at 05:38
  • 5
    I think instead of adding to TEMPLATE_CONTEXT_PROCESSORS, which is deprecated since 1.8, you should add to the OPTIONS.context_processors list in TEMPLATES. – tscizzle Oct 10 '15 at 06:03
  • @VictorCastilloTorres is there a way to this in Django 3.x ? – Poula Adel Apr 14 '20 at 13:45
  • 1
    @PoulaAdel check this: https://docs.djangoproject.com/en/3.0/ref/templates/api/ – Victor Castillo Torres Apr 21 '20 at 15:08