Unable to get custom context processor to be invoked
Asked Answered
K

1

8

I am trying to create a custom context processor which will render a list of menu items for a logged in user. I have done the following:


Within my settings.py I have

TEMPLATE_CONTEXT_PROCESSOR = (
    'django.contrib.auth.context_processors.auth',
    'mysite.accounts.context_processors.user_menu',
)

Under the accounts submodule I have context_processors.py with the following, for now:

def user_menu(request):
    return {'user_menu':'Hello World'}

On my template page I have the following:

{% if user.is_authenticated %}
Menu
{{user_menu}}
{% endif %}

The invoking view is as follows:

def profile(request):
    return render_to_response('accounts/profile.html',context_instance=RequestContext(request))

However I am unable to get the {{user_menu}} to render anything on the page, I know the user is authenticated as other sections of the template with similar checks render correctly. Am I missing something here. Please help Thank you

Edit: Thanks Ben, Daniel, I have fixed the (S) in TEMPLATE_CONTEXT_PROCESSOR, however Django now has trouble resolving the module and I get the following message

Error importing request processor module django.contrib.auth.context_processors: "No module named context_processors"

UPDATE: I fixed it by changing the path to django.core.context_processors.auth Seems like the modules have been moved around

Katlaps answered 27/2, 2010 at 20:57 Comment(1)
It should be TEMPLATE_CONTEXT_PROCESSORSChordophone
L
5

The setting name should be TEMPLATE_CONTEXT_PROCESSORS, with an S.

Locality answered 27/2, 2010 at 21:0 Comment(2)
Ah, Thanks... :p.. I seem to have progressed a bit more now I get the following error.. Sorry I am a newbie to both python and django Error importing request processor module django.contrib.auth.context_processors: "No module named context_processors"Katlaps
Says you fixed it by make sure every folder you create with python code has a init.py in it. That tells python, yo, it's a module.Sulfonal

© 2022 - 2024 — McMap. All rights reserved.