All Articles

Want user available in every template in Django?

Create a context processor

settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
	'myapp.context_processors.user',
)

context_processors.py

def user(request):
	if hasattr(request, 'user'):
		return {'user':request.user }
	return {}

user is now available in any template:

{% verbatim %}
{% if user.is_authenticated %}Do Something Special{% endif %}
{% endverbatim %}

source: StackOverflow

Published 29 Dec 2010

Code. Comics. Crossfit
Tim Broder on Twitter