创建目录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))
效果:
本文出自 “LannyMa” 博客,请务必保留此出处http://lannyma.blog.51cto.com/4544390/1735756
原文:http://lannyma.blog.51cto.com/4544390/1735756