Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my django template context processor not getting called

I must have missed something in setting up a custom template context as it's never getting called.

In settings:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django_authopenid.context_processors.authopenid",
    "web.context_processors.my_hat",
)

in web/context_processors.py

from libs.utils import get_hat, get_project, my_hats

print 'heloooo'

def my_hat(request):
    """Insert some additional information into the template context
    """
     
    import pdb
    pdb.set_trace()

    print 'hiiiiiiii'

    return {'hat': get_hat(request),
        'project': get_project(request),
        }

nothing is output and django processes view and displays template without ever hitting this. What have I missed!?

Thanks Insin, the bits I had missed:

In the view.py

return render_to_response(template, {
        'tasks': tasks,
    },
    context_instance=RequestContext(request))

In the template:

  My current hat is {{hat}}
like image 456
PhoebeB Avatar asked Oct 06 '09 19:10

PhoebeB


1 Answers

Did you remember to use RequestContext when rendering the template?

As of Django 1.3, there is a new shortcut function, render, which uses RequestContext by default: :

return render(request, template, {
    'tasks': tasks,
})
like image 60
Jonny Buchanan Avatar answered Oct 22 '22 08:10

Jonny Buchanan