views:

28

answers:

1

I have this in my base.html template:

{% if user.is_authenticated %}
<p style="color:#000;">
    Welcome
    {{ user.first_name }}
    | 
    <a href="/logout/">Logout</a>
</p>
{% endif %}

After I authenticate, and go to this page, my first name does not show up. Can anybody tell me why?

The "Welcome" also does not show up. So, this must be failing on user.is_authenticated.

Thanks. :)

+1  A: 

The django.contrib.auth.context_processors.auth middleware is responsible for setting request.user before a request reaches the controller.

In order to access it from a template, you need to pass this variable or a RequestContext. Example:

def something(request):
    context_instance = RequestContext(request)
    template_name = 'your_template.html'
    extra_context = { 'other_variable': 'some value' }
    return render_to_response(template_name, extra_context, context_instance)

This way, all the request variables are accessible from the template.

jweyrich
Very nice. I used this code exactly. :)
Eric

related questions