#views.py定义的视图
from django.contrib.auth import authenticate,login
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request,user)
return HttpResponseRedirect('/homepage/') #回到主页
else:
#返回账户invalid的消息
else:
#返回login页面
#在主页的显示html文件中,使用is_authenticated先判断了用户是否登录了,如下:
{% if user.is_authenticated %}
<h5>>Hello {{ user.username }} !</h5>
{%else%}
<h5>Weclome !</h5>
{%endif%}
问题是:在user_login视图里,使用login后,能正常返回到主页,说明登录成功,可是在主页中,使用is_authenticated判定后,总是为False,总是无法显示已登录的状态。
环境:Python 2.7,Django:1.10