booktest-11 requst的使用

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中間件即可正常運行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章