django之從html頁面表單獲取輸入的數(shù)據(jù)實(shí)例
本文主要講解如何獲取用戶在html頁面中輸入的信息。
1.首先寫一個自定義的html網(wǎng)頁
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'%} 對應(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)入主頁localhost:8000 ,輸入用戶名密碼,點(diǎn)擊提交
這時會報403錯誤

需要在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ù),再次輸入用戶名密碼
就可以得到在頁面輸入的信息了

以上這篇django之從html頁面表單獲取輸入的數(shù)據(jù)實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)水仙花數(shù)實(shí)例講解
這篇文章主要介紹了python實(shí)現(xiàn)水仙花數(shù)實(shí)例講解,有正在學(xué)習(xí)python的同學(xué)可以跟著小編一起來學(xué)習(xí)下水仙花數(shù)怎么用python計算吧2021-03-03
Python實(shí)現(xiàn)如何根據(jù)文件后綴進(jìn)行分類
本文主要為大家詳細(xì)介紹了如何通過python實(shí)現(xiàn)根據(jù)文件后綴實(shí)現(xiàn)分類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以關(guān)注一下2021-12-12
Python實(shí)現(xiàn)多并發(fā)訪問網(wǎng)站功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)多并發(fā)訪問網(wǎng)站功能,結(jié)合具體實(shí)例形式分析了Python線程結(jié)合URL模塊并發(fā)訪問網(wǎng)站的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

