User authentication in Django. Problems with is_authenticated
Posted
by
tim
on Stack Overflow
See other posts from Stack Overflow
or by tim
Published on 2012-10-25T10:46:45Z
Indexed on
2012/10/25
11:00 UTC
Read the original article
Hit count: 220
django
|authentication
I have one problem with users menu. So, I want, that authenticated user can see his/her profile page and logout (links) in menu. It works (when I logging in) on index page: index, page1, profile, logout ,but, if I go to the, for example, page1 I can see in menu: index, page1, login, not profile and logout. How to fix it?
in urls:
url(r'^accounts/login/$', 'django.contrib.auth.views.login' ),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login' ),
url(r'^accounts/profile/$', 'my_app.views.profile' ),
in views:
def profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect("/accounts/login/")
else:
user = request.user.is_authenticated()
return render_to_response('profile.html',locals())
Part of index.html:
{% if user.is_authenticated or request.user.is_authenticated %}
<li><a href="/accounts/profile/">Profile</a></li>
<li><a href="/accounts/logout/">logout</a></li>
{% else %}
<li><a href="/accounts/login/">login</a></li>
{% endif %}
login.html:
{% extends "index.html" %}
{% load url from future %}
{% block application %}
{% if form.errors %}
<p>Try one more time</p>
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="Login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
profile.html:
{% extends "index.html" %}
{% block application %}
{% if request.user.is_authenticated %}
<p>Welcome, {{ request.user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
{% endblock %}
© Stack Overflow or respective owner