Skip to content Skip to sidebar Skip to footer

Django Writing Custom Context Processor

I'm writing my own custom context_processor on django (1.11) and to get infos of an authenticated user from auth0. It's not my first time writing it and I don't understand where th

Solution 1:

Instead of

'auth.context_processors.auth0_processors'

give the concrete method:

'auth.context_processors.auth0_processors.auth0_user'

At least that is what the error is complaining about:

does not define a "auth0_processors" attribute/class

It is looking for a class or attribute, so try with the function name.

From the documentation:

The context_processors option is a list of callables – called context processors – that take a request object as their argument and return a dictionary of items to be merged into the context.

In answer to your comment:

If you always need the same objects then just create one method that adds all of the required objects to the context instead of several methods.

EDIT:

Also note that with 'django.template.context_processors.request' you could already have the complete request object in the context. You might not need your own context processor if you just need access to the session.

Post a Comment for "Django Writing Custom Context Processor"