標籤:get null 提示 ror orm cti html .post 錯誤提示
#Now 讓我們繼續對上篇的登入進行操作
#對於csrf,以後再開篇章記錄
#修改index.html
<form method="post" action="/login_action/">
#修改urls.py,添加login_action/的路徑
url(r‘^login_action/$‘,views.login_action),、
#登入請求由views.py視圖檔案的login_action函數來處理,建立login_action視圖函數,注意
def login_action(request):
if request.method==‘POST‘:
username=request.POST.get(‘username‘,‘‘)
password=request.POST.get(‘password‘,‘‘)
if username==‘admin‘ and password==‘123456‘:
return HttpResponse(‘login success‘)
elif username==‘‘ or password==‘‘:
return render(request,‘index.html‘,{‘error1‘:‘username or password is null!‘})
else:
return render(request,‘index.html‘,{‘error2‘:‘username or password error!‘})
#重新訪問,注意不要注釋之前的index方法,如注釋,則找不到index頁面,同時也訪問不到/login_action
#但實際上error1和error2是沒有顯示的
#修改index.html
#表單下方添加
{{error1}}<br>
{{error2}}<br>
#到此,輸入空,或密碼錯誤會根據分別顯示error1/error2的錯誤提示
Python Django 2.2登入功能_2