django之從html頁(yè)面表單獲取輸入的數(shù)據(jù)實(shí)例
本文主要講解如何獲取用戶在html頁(yè)面中輸入的信息。
1.首先寫一個(gè)自定義的html網(wǎng)頁(yè)
login.html
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>test</title></head><body> <form method='post' action='{% url ’check’ %}'> <input type='text' name='name' placeholder='your username'><br> <input type='password' name='pwd' placeholder='your password'><br> <input type='submit' value='提交'><br> </form></body></html>
form表單里的action{%url ‘check’%} 對(duì)應(yīng)的是urls.py里的name值
2.配置urls.py文件
urlpatterns = [ path(’reg/’,views.reg,name=’check’), path(’’,views.login),]
3.配置views.py文件
def login(request): return render(request,’login.html’)def reg(request): if request.method == ’POST’: name=request.POST.get(’name’) pwd=request.POST.get(’pwd’) print(name,pwd) return render(request,’login.html’)
4.開啟服務(wù),進(jìn)入主頁(yè)localhost:8000 ,輸入用戶名密碼,點(diǎn)擊提交
這時(shí)會(huì)報(bào)403錯(cuò)誤
需要在login.html文件的form表單中加入下面一行代碼
{%csrf_token%} <form method='post' action='{% url ’check’ %}'> {% csrf_token %} <input type='text' name='name' placeholder='your username'><br> <input type='password' name='pwd' placeholder='your password'><br> <input type='submit' value='提交'><br> </form>
重啟服務(wù),再次輸入用戶名密碼
就可以得到在頁(yè)面輸入的信息了
以上這篇django之從html頁(yè)面表單獲取輸入的數(shù)據(jù)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Hybris在idea中debug配置方法詳解2. 在idea中為注釋標(biāo)記作者日期操作3. jsp cookie+session實(shí)現(xiàn)簡(jiǎn)易自動(dòng)登錄4. XPath入門 - XSL教程 - 35. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效6. .NET Core Web APi類庫(kù)內(nèi)嵌運(yùn)行的方法7. .NET6使用ImageSharp實(shí)現(xiàn)給圖片添加水印8. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車9. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)10. .net如何優(yōu)雅的使用EFCore實(shí)例詳解
