Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - template context processors - breaking my app

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.

like image 996
Greg Avatar asked May 23 '11 15:05

Greg


People also ask

What is template context processors in Django?

context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dict of items to be merged into the context.

Is Django template slow?

The big disclaimer on something like this is: Django templates are slow, but does it really matter for your particular usage case? CPython is much slower than C/C++, but if you use it for appropriate things and engineer correctly, it's "fast enough".

What does safe do in Django template?

Safe is used when you want to turn it off and let the template rendering system know that your date is safe in an un-escaped form. See the django docs for details: Automatic HTML escaping.

Do Django templates have multiple extends?

Yes you can extend different or same templates.


1 Answers

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",
)
like image 132
PiGGeH Avatar answered Sep 28 '22 09:09

PiGGeH