欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django之從html頁(yè)面表單獲取輸入的數(shù)據(jù)實(shí)例

 更新時(shí)間:2020年03月16日 08:49:43   作者:5sh  
這篇文章主要介紹了django之從html頁(yè)面表單獲取輸入的數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

本文主要講解如何獲取用戶在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 ,輸入用戶名密碼,點(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論