[Django]-Always including the user in the django template context

20👍

In a more general sense of not having to explicitly set variables in each view, it sounds like you want to look at writing your own context processor.

From the docs:

A context processor has a very simple interface: It’s just a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Each context processor must return a dictionary.

46👍

There is no need to write a context processor for the user object if you already have the "django.core.context_processors.auth" in TEMPLATE_CONTEXT_PROCESSORS and if you’re using RequestContext in your views.

if you are using django 1.4 or latest the module has been moved to django.contrib.auth.context_processors.auth

👤Daniel

33👍

@Ryan: Documentation about preprocessors is a bit small

@Staale: Adding user to the Context every time one is calling the template in view, DRY

Solution is to use a preprocessor

A: In your settings add

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

B: In myapp/processor_file_name.py insert

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

From now on you’re able to use user object functionalities in your templates.

{{ user.get_full_name }}
👤zalun

3👍

The hints are in every answer, but once again, from “scratch”, for newbies:

authentication data is in templates (almost) by default — with a small trick:

in views.py:

from django.template import RequestContext
...
def index(request):
    return render_to_response('index.html', 
                              {'var': 'value'},
                              context_instance=RequestContext(request))

in index.html:

...
Hi, {{ user.username }}
var: {{ value }}
... 

From here: https://docs.djangoproject.com/en/1.4/topics/auth/#authentication-data-in-templates

This template context variable is not available if a RequestContext is
not being used.

2👍

If you can hook your authentication into the Django authentication scheme you’ll be able to use request.user.

I think this should just be a case of calling authenticate() and login() based on the contents of your Cookie.

Edit: @Staale – I always use the locals() trick for my context so all my templates can see request and so request.user. If you’re not then I guess it wouldn’t be so straightforward.

2👍

@Dave
To use {{user.username}} in my templates, I will then have to use
requestcontext rather than just a normal map/hash: http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext

So I guess there are no globals that the template engine checks.

But the RequestContext has some prepopulate classes that I can look into to solve my problems. Thanks.

👤Staale

1👍

its possible by default, by doing the following steps, ensure you have added the context ‘django.contrib.auth.context_processors.auth’ in your settings. By default its added in settings.py, so its looks like this

TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.auth',)

And you can access user object like this,

{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}

For more information, refer here http://docs.djangoproject.com/en/1.2/topics/auth/#authentication-data-in-templates

0👍

Use context_processors. https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-TEMPLATES-OPTIONS

settings.py

'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'myapp.functions.test'
        ],
},

myapp/functions.py

def test(request):
    return {'misc': 'misc'}
👤synkc

Leave a comment