functools <span style="color: #0000ff;">def<span style="color: #000000;"> check_login(func):
@wraps(func)
<span style="color: #0000ff;">def inner(request,
<span style="color: #000000;">kwargs):
next_url =<span style="color: #000000;"> request.get_full_path()
<span style="color: #0000ff;">if request.session.get(<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"<span style="color: #000000;">):
<span style="color: #0000ff;">return func(request,<span style="color: #000000;">kwargs)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return redirect(<span style="color: #800000;">"<span style="color: #800000;">/login/?next={}<span style="color: #800000;">"<span style="color: #000000;">.format(next_url))
<span style="color: #0000ff;">return<span style="color: #000000;"> inner
<span style="color: #0000ff;">def<span style="color: #000000;"> login(request):
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">"<span style="color: #800000;">POST<span style="color: #800000;">"<span style="color: #000000;">:
user = request.POST.get(<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"<span style="color: #000000;">)
pwd = request.POST.get(<span style="color: #800000;">"<span style="color: #800000;">pwd<span style="color: #800000;">"<span style="color: #000000;">)
</span><span style="color: #0000ff;">if</span> user == <span style="color: #800000;">"</span><span style="color: #800000;">alex</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">and</span> pwd == <span style="color: #800000;">"</span><span style="color: #800000;">alex1234</span><span style="color: #800000;">"</span><span style="color: #000000;">:
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 设置session</span>
request.session[<span style="color: #800000;">"</span><span style="color: #800000;">user</span><span style="color: #800000;">"</span>] =<span style="color: #000000;"> user
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 获取跳到登陆页面之前的URL</span>
next_url = request.GET.get(<span style="color: #800000;">"</span><span style="color: #800000;">next</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 如果有,就跳转回登陆之前的URL</span>
<span style="color: #0000ff;">if</span><span style="color: #000000;"> next_url:
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> redirect(next_url)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 否则默认跳转到index页面</span>
<span style="color: #0000ff;">else</span><span style="color: #000000;">:
</span><span style="color: #0000ff;">return</span> redirect(<span style="color: #800000;">"</span><span style="color: #800000;">/index/</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">"</span><span style="color: #800000;">login.html</span><span style="color: #800000;">"</span><span style="color: #000000;">)
@check_login
<span style="color: #0000ff;">def<span style="color: #000000;"> logout(request):
<span style="color: #008000;">#<span style="color: #008000;"> 删除所有当前请求相关的session
<span style="color: #000000;"> request.session.delete()
<span style="color: #0000ff;">return redirect(<span style="color: #800000;">"<span style="color: #800000;">/login/<span style="color: #800000;">"<span style="color: #000000;">)
@check_login
<span style="color: #0000ff;">def<span style="color: #000000;"> index(request):
current_user = request.session.get(<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"<span style="color: #000000;">,None)
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">"<span style="color: #800000;">index.html<span style="color: #800000;">",{<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">": current_user})