django之從html頁(yè)面表單獲取輸入的數(shù)據(jù)實(shí)例
本文主要講解如何獲取用戶(hù)在html頁(yè)面中輸入的信息。
1.首先寫(xiě)一個(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.開(kāi)啟服務(wù),進(jìn)入主頁(yè)localhost:8000 ,輸入用戶(hù)名密碼,點(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ù),再次輸入用戶(hù)名密碼
就可以得到在頁(yè)面輸入的信息了
以上這篇django之從html頁(yè)面表單獲取輸入的數(shù)據(jù)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python中scrapy處理項(xiàng)目數(shù)據(jù)的實(shí)例分析2. 快速搭建Spring Boot+MyBatis的項(xiàng)目IDEA(附源碼下載)3. js抽獎(jiǎng)轉(zhuǎn)盤(pán)實(shí)現(xiàn)方法分析4. IntelliJ IDEA導(dǎo)入jar包的方法5. Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)6. GIT相關(guān)-IDEA/ECLIPSE工具配置的教程詳解7. 教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程8. 深入分析PHP設(shè)計(jì)模式9. 如何基于Python實(shí)現(xiàn)word文檔重新排版10. vue-electron中修改表格內(nèi)容并修改樣式
