1.requestQueryDict的用法,get時候不存在,不會報錯,而且可以指定默認值
2.增加登陸視圖函數
def login(request):
return render(request, 'booktest/login.html')
def login_check(request):
username = request.POST.get('username')
password = request.POST.get('password')
print(username+":"+password)
if username == 'admin' and password == '111':
return redirect('/index')
else:
return redirect('/login')
3.在templates下面添加login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸頁面</title>
</head>
<body>
<form method="post" action="/login_check">
用戶名:<input type="text" name="username"/><br/>
密碼:<input type="password" name="password"/><br/>
<input type="submit" value="登陸">
</form>
</body>
</html>
4.當點擊登陸時,會出現如下界面,
5.註釋掉settings.py裏面的csrf中間件即可正常運行