我对用户菜单有一个问题。因此,我希望经过身份验证的用户可以在菜单中看到他/她的个人资料页面和注销(链接)。它在索引页面上工作(当我登录时):index, page1, profile, logout,但是,如果我转到 page1 ,我可以在菜单中看到:index, page1, login,而不是 profile 和 logout 。如何解决?
在网址中:
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' ),
在意见中:
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())
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 %}
登录.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 %}
page1.html:
{% extends "index.html" %}
{% block application %}
some for loops here
{% endblock %}
第 1 页的浏览量:
def car(request):
all_cars = Car.objects.all().filter(active=1).values('id', 'name')
return render_to_response('page1.html', {'all_cars': all_cars})