Search code examples
djangopython-3.xdjango-templatesdjango-viewsdjango-context

Django ImportError No module named 'catalog.context_processors'


I'm a Django beginner and I encounter an issue with django context_processors. I want use a queryset in all my template for generate a menu. But I get this error when I try to reach this page http://mysite/catalog which calls my cardabelle/catalog/views.py :

ImportError at /catalog/
No module named 'cardabelle.catalog'

Here "cardaballe" is my project name and "catalog" my application name.

Here is some interesting part (i guess) from my cardabelle/cardabelle/settings.py :

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'debug_toolbar',
    'catalog',
    'autoslug',
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'template')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'cardabelle.catalog.context_processors.categories',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'cardabelle.wsgi.application'

and here is my custom context in cardabelle/catalog/views.py :

def categories(request):
    return Category.objects.value()

Somebody knows why django doesn't find my new custom context ?

Thanks in advance for your help !


Solution

  • I guess you have the file context_processors.py in the directory catalog, which is on the same level as the directory cardabelle?

    /catalog
        __init__.py
        context_processors.py
        ...
    /cardabelle
        __init__.py
        settings.py
        ...
    

    If yes, the context_processor setting should read

    TEMPLATES = [
        {
            # ...
            'OPTIONS': {
                'context_processors': [
                    # ...
                    'catalog.context_processors.categories',
                    # ...
                ],
            },
        },
    ]
    

    Also the context_processor should return a dict. Your current code reads Category.objects.value(). This is probably a typo while pasting it to SO? Just in case, make sure it reads Category.objects.values(), which returns a list of dicts.

    def categories(request):
        return {'menu_categories': Category.objects.values()}
    

    It will then be available as {{ menu_categories }} in your templates.