Devinwon / article

0 stars 0 forks source link

Django2.0——根据用户身份状态变化 #16

Open Devinwon opened 6 years ago

Devinwon commented 6 years ago

简单的示例

{% if username is None %}
<div class="item"> <a href="/account/login">登录|注册</a></div>
{% else %}
<div class="item"> <a href="/account/logout">退出</a></div>
{% endif %}

views.py

#POST
if userResult and userResult.is_active:
    response=redirect(to='/')
    response.set_cookie("username",username,max_age=3600)
    context["username"]=username
    return response
....
#GET
if request.method=="GET":
     # request.COOKIES["username"]
     username=request.COOKIES.get("username",None)
     context["username"]=username
     return render(request,"login.html",context)