I need a link in django template which turns into logout if user is authenticated. (i have already implemented login/logout pages)
tried {% if user.is_authenticated %} {% endif %}
and {% if user.is_anonymous %} {% endif %}
but didn't work.
Test Code (https://docs.djangoproject.com/en/dev/topics/auth/) -
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
Returns false evan after logged in successfully.
user.is_authenticated
set correctly, or is it always True
- Blender 2012-04-04 02:56
{% if user.is_authenticated %}
always returns false - ChamingaD 2012-04-04 02:58
{% if user.is_authenticated %}
work - ChamingaD 2012-04-04 03:41
It doesn't look like there is anything wrong with the template code you posted. So I'd check out the associated view. In particular, if you're using a custom-made view (rather than, say, a generic view), remember to supply a RequestContext to your template.
From the Django tutorial, part 4:
from django.template import RequestContext
# ...
def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('polls/detail.html', {'poll': p},
context_instance=RequestContext(request))
context_instance=RequestContext(request)
ChamingaD 2012-04-04 03:49
{% else %}
as well - Blender 2012-04-04 02:51