首页 > 其他 > 详细

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

时间:2016-01-17 16:19:16      阅读:198      评论:0      收藏:0      [点我收藏+]

创建目录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

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

原文:http://lannyma.blog.51cto.com/4544390/1735756

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!