Django add query-based context to all views

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

+4
source share
1 answer

Just add a context handler that will add this variable to the context. Context processor - 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',
    ...
)
+12
source

Source: https://habr.com/ru/post/1616058/


All Articles