创建目录templates:

myform/
├── db.sqlite3
├── manage.py
├── myform
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── tools
    ├── admin.py
    ├── __init__.py
    ├── models.py
    ├── templates #注意这里路径
    │   └── index.html
    └── views.py

表单定义:index.html

<!DOCTYPE html>
<html>
<body>
<p>请输入两个数字</p>
<form action="/add/" method="get"> //注意这里/add/
    a: <input type="text" name="a"> <br>
    b: <input type="text" name="b"> <br>
    <input type="submit" value="提交">
</form> 
</body>
</html>

修改urls.py

url(r"^$",views.index),
url(r"^add/$",views.add),

修改视图

首次访问,index.html.点提交后,获取到新的url,随之出来结果。

from django.http import HttpResponse
from django.shortcuts import render
 
def index(request):
    return render(request, 'index.html')
  
def add(request):
    a = request.GET['a']
    b = request.GET['b']
    a = int(a)
    b = int(b)
    return HttpResponse(str(a+b))


效果:

django 通过表单传递数据到后台_表单

django 通过表单传递数据到后台_提交数据_02