a 在views中导入
from django.contrib.auth.decorators import login_required
@login_required()
b 在views 导入
from django.contrib.auth import authenticate
def login_mlj(request):
if request.method =="POST":
user=authenticate(username=request.POST["username"],
password=request.POST["password"]
)
if user is not None:
login(request,user)
print( request.POST)
return redirect('/app1/index')
return render(request,"login.html")
c 显示用户名
{% if request.user.is_authenticated %}
{{ request.user }}
{% endif %}
d 退出
from django.contrib.auth import authenticate,login,logout、
logout(request)