Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django add context based on request to all views

Tags:

django

I need to render my site without navbar if there is an argument in the request like nv=false and I want to pass a variable to context based on this so that the main template shows the block or not. my site also has lots of json-rpc functions, and I don't want to add extra overhead on it. how can I do this without rewriting all my views? (they are not class based and my site uses django 1.8)

like image 653
Mohibeyki Avatar asked Nov 16 '15 13:11

Mohibeyki


1 Answers

Just add context processor that will add this variable to context. Context processor is a simple python function

def navbar(request):
    return {'navbar_enabled': request.GET.get('nv', False)

and add it to the list of template context processors

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'MODULE_NAME.navbar',
    ...
)
like image 112
Nick Avatar answered Nov 11 '22 03:11

Nick