Python Django 2.2登入功能_2

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.