Search code examples
djangodjango-templatesdjango-viewsdjango-context

why is context processor adding to every view in django?


I have created a context processor so that I have a variable passed to all of my templates.

My settings.py looks like this:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'myapp.context_processors.setting_processor',
)

In a single view I pass:

request_context = RequestContext(request,
            processors=[setting_processor])

    return render(request, 'settings.html', context, context_instance=request_context)

I also, put in my processor and it triggers in every view of my site. I thought it only is hit when I pass in the request context from a view?

Here is the code that prints in every view I go to:

def setting_processor(request):
    print '--------------- in content processor'

How do I make it only trigger the context processor in certain views?


Solution

  • When you add a processor to settings it is passed in every view.

    If you want only in a single view remove from settings and use what I have above:

    request_context = RequestContext(request,
                processors=[setting_processor])
    
        return render(request, 'settings.html', context, context_instance=request_context)