0

I have project in Django 1.3. In order to show username in all pages I use such tags in base.html

{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}.  
    <a href="/proc/accounts/logout/">loggout</a></p>
{% else %}
    <a href="/proc/accounts/login/">loggin</a>
{% endif %}

But if I dont return context_instance=RequestContext(request) from view value of user in template is empty. The 'django.contrib.auth.context_processors.auth' is included to TEMPLATE_CONTEXT_PROCESSORS.

Is it possible automaticaly include user to all templates?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Daler
  • 814
  • 14
  • 28

3 Answers3

1

You've given the answer yourself. As long as you use a RequestContext, it will be included in all templates.

If you really find that too much work, you could use the (new in 1.3) TemplateResponse class.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

since django 1.3. use shortcuts.render function and dont warry about requestcontext including to your views

-1

Or simply create a context processor. See http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

Put this in context_processor.py

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

in settings.py add the context processor.

now in your template try: {{ user }}

Roel Kramer
  • 371
  • 3
  • 12