Django - template context processors - breaking my app
Asked Answered
D

1

15

I was trying to set up a template context processor like this article mentions so that I could provide information to every template.

I wrote this function in views.py:

def items_in_cart(request):
    """Used by settings.TEMPLATE_CONTEXT_PROCESSORS to provide an item count
    to every template"""
    cart, lines = get_users_cart_and_lines(request)
    return {'items_in_cart': lines.count()}

And then I added this line to settings.py:

TEMPLATE_CONTEXT_PROCESSORS = ('Store.views.items_in_cart',)

But now whenever I go to any page I get this error:

ImproperlyConfigured at /

Put 'django.contrib.auth.context_processors.auth' in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.

Did I do something wrong? What's going on here? I tried doing what the error said, and then it will render a page with all of my style sheets and images missing.

Dillman answered 23/5, 2011 at 15:25 Comment(0)
B
21

Django has a default set of TEMPLATE_CONTEXT_PROCESSORS, which you need to manually add when adding your own. http://docs.djangoproject.com/en/1.3/ref/settings/#template-context-processors

Depending on your Django version these are different, however if using Django 1.3 you might have something as follows

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.contrib.messages.context_processors.messages",
    "Store.views.items_in_cart",
)
Biestings answered 23/5, 2011 at 15:31 Comment(4)
hmm, can I just add mine to the existing default list? E.g., TEMPLATE_CONTEXT_PROCESSORS = secret_default_location.TEMPLATE_CONTEXT_PROCESSORS + ('Store.views.items_in_cart',)Dillman
You should be able to use TEMPLATE_CONTEXT_PROCESSORS += ('Store.views.items_in_cart',) to append to the defaultsBiestings
The default django settings can be imported using from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS above your line to append. You can then append to this using TEMPLATE_CONTEXT_PROCESSORS += ('Store.views.items_in_cart',)Biestings
In Django 1.10 the value is removed from global_settings and the new TEMPLATES setting should be used. How should code like the above which adds to the default setting be migrated? Should they just be included manually? Is there even a default any more?Iong

© 2022 - 2024 — McMap. All rights reserved.