Search code examples
djangodjango-templatesdjango-context

Custom context processor doesn't work


I have the following code:

1) context processor

def defaults(request):
    return {
        'LOGO_DEFAULT_SRC': LOGO_DEFAULT_CSRC
    }

2) settings

  'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                # custom processors
                'apps.core.context_processors.defaults',
            ],

3) in template

{{ defaults.LOGO_DEFAULT_SRC }}

4) I use Generic Class Based View.

I don't understand why this isn't working. I restarted the server, cleaned .pyc files.


Solution

  • When you create a context processor, the dictionary that you return in the defaults function is added to the global context, so you can access this in your template just by its name.

    Instead of using:

    {{ defaults.LOGO_DEFAULT_SRC }}
    

    you need to use:

    {{LOGO_DEFAULT_SRC}}