Reputation: 4533
In my base.html file, I am using
{% if user.is_authenticated %}
<a href="#">{{user.username}}</a>
{% else %} <a href="/acc/login/">log in</a>
Here, even if the user is logged in, the log in button shows up.
Now when I click on the log in
link, it shows the username and also the normal login view, saying user is logged in.
So, what's wrong?
Upvotes: 2
Views: 781
Reputation: 99841
Sounds like you're not getting any user information in your templates. You need 'django.contrib.auth.middleware.AuthenticationMiddleware'
in your MIDDLEWARE_CLASSES
setting, and to get that goodness in context for your templates, you need to do:
from django.shortcuts import render_to_response
from django.template import RequestContext
def my_view(request):
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
To save you doing this everywhere, consider using django-annoying's render_to
decorator instead of render_to_response
.
@render_to('template.html')
def foo(request):
bar = Bar.object.all()
return {'bar': bar}
# equals to
def foo(request):
bar = Bar.object.all()
return render_to_response('template.html',
{'bar': bar},
context_instance=RequestContext(request))
Upvotes: 5
Reputation: 4353
I am sure that the answer of Dominic Rodger solves your issue. Just wanted to add that I personally prefer to import direct_to_template
instead of render_to_response
:
from django.views.generic.simple import direct_to_template
...
return direct_to_template(request, 'my_template.html', my_data_dictionary)
but I guess it's just a matter of taste. In my case you could also use named parameters instead of my_data_dictionary
:
return direct_to_template(request, 'template.html', foo=qux, bar=quux, ...)
Upvotes: 1