passing a variable to all templates from views.py in django

时间:2017-06-15 10:11:59

标签: python django django-views

is that possible in django to pass a variable from views.py to all html templates.

for example: i have a variable "baseURL" it will be used in all templates. the reason is "baseURL" keeps on changing and if i change this in one place it should be reflected in all the places

2 个答案:

答案 0 :(得分:4)

This is what context processors are for.

def base_url(request):
    return {'base_url': settings.BASE_URL}

and add path.to.module.base_url to the TEMPLATES context_processors list in your settings; now you can do {{ base_url }} in any template.

答案 1 :(得分:0)

from django.shortcuts import render

def search_isbn(request):
    var = 0
    if request.method == 'POST':
        var = 500
    return render('results.html', {'var':var})