Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass variables to all templates in django? [duplicate]

I am trying to pass variables (browser variable) to all my templates in my app. Any advice on how to get it to work?

View:

def browser(request):
    primary_cat_list = Categories.objects.order_by("category")
    subcat_list = SubCategories.objects.order_by("sub_category")
    product = Productbackup.objects.order_by("website")
    browser =  list(chain(primary_cat_list, subcat_list, product))
    return render_to_response('reserve/templates/base.html', locals(), context_instance=RequestContext(request))

Template:

{% for prod in browser %} {{ prod }}, {% endfor %}
like image 463
sharataka Avatar asked Aug 19 '12 22:08

sharataka


People also ask

What is a more efficient way to pass variables from template to view in Django?

POST form (your current approach) This answer is perfect and I learned a great deal!

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What does {% endblock %} mean?

html' %} . {% block %}{% endblock %}: This is used to define sections in your templates, so that if another template extends this one, it'll be able to replace whatever html code has been written inside of it. Blocks are identified by their name.


2 Answers

You, my friend, are in the market for Context Processors.

From a blog entry written by a far nimbler and erudite technical writer than I:

What are template context processors?

Django’s context processors are a facility that allows you to provide data and callbacks to your templates.

You can do so in one of two ways:

  • On an individual request basis: by passing a custom Context value to your render_to_response() call
  • Globally: by creating a context processor method that accepts a HttpRequest object as input, and returns a payload or callback, then registering the context processor in your settings.py, then providing your render_to_response() call with the built-in RequestContext attribute instead of your own (you can always extend RequestContext to add more data on an individual request basis of course).

If that approach for passing data to templates sounded absurd and obfuscated to you, you’re not alone. The complexity involved in such a simple operation is unwarranted and counter-productive, but every system has its shortcomings.

The official documentation is here:

https://docs.djangoproject.com/en/dev/ref/templates/api/

So but yeah, I have been programming with Django for a while, and one of the reasons I really like solving problems w/ it is because it is almost Byzantine in its complexity, but not in a domineering sort of way. It has a ton of geegaws and doodads that may not immediately appear useful; each of these either comes in extremely handy when you need it, and it will stay out of your way if not.

The upshot here for you is: context processors are a fine example of those. Yes.

like image 172
fish2000 Avatar answered Oct 30 '22 15:10

fish2000


I can give you an example of my code, that works fine. Here is the file named context_processors.py:

context_processors.py

def base(request):
    user = request.user

#======================
#Login form
#=====================

# here is the code for login user or check if he is logged in already

return  {
        'user': user,
        }

and that's, part of my base.html (a template that I use wor all my pages)

base.html

{% if user.username %}
                    <h3>
                        Welcome {{ user.username }}
                    </h3>
like image 20
Vor Avatar answered Oct 30 '22 15:10

Vor