我只想要一个简单的 django 用户会话 auth 。登录,用户数据,注销。还可以在模板中测试会话。代码:
def auth_view(request):
username = request.POST.get('username','')
password = request.POST.get('password','')
user = auth.authenticate(username = username, password = password)
if user is not None:
if user.is_active:
auth.login(request,user)
return HttpResponseRedirect('/accounts/loggedin')
else:
return HttpResponseRedirect('/accounts/auth_view')
else:
return HttpResponseRedirect('/accounts/invalid')
def loggedin(request):
request.session['username'] = request.user.username //( tried this)<----------
return render_to_response('loggedin.html',
{ 'username' : request.user.username})
def logout(request):
auth.logout(request)
(kill the session) <-----------
return render_to_response('logout.html')
基本模板(所有模板都扩展了这个):...
<div id="rightsidebar">
{% block rightsidebar %}
{% if request.session.username != Null %} // (tried this) <-----------
Loggedin
{% else %}
Not Loggedin
{% endif %}
{% endblock %}
</div>
<div id="content">
{% block content %}This is the content area{% endblock %}
...