以下例子是“Django basic:views-URL”部分的延伸。
Demo1
# learn/urls.py
url(r'^index/$',views.index,name='index'),
# learn/views.py
def index(request):
#return HttpResponse(u"欢迎访问 我的Django学习记录")
return render(request,'add.html')
# learn/templates/add.html
# 网页的值传到服务器是通过 <input> 或 <textarea>标签中的 name 属性来传递的
<p>请输入两个数字</p>
<form action="{% url 'learn:add' %}" method="get">
a: <input type="text" name="a"> <br>
b: <input type="text" name="b"> <br>
<input type="submit" value="提交">
</form>
# learn/urls.py
url(r'^add/$',views.add,name="add"),
# learn/views.py
# 在服务器端接受表单页面name属性传来的值,可以用 request.GET.get('key', None)来取值,没有时不报错。
def add(request):
a = request.GET['a']
b = request.GET['b']
c = int(a)+int(b)
return HttpResponse(str(c))
第一次请求:浏览器访问127.0.0.0:8000/learn/index,渲染模板add.html;
鼠标点中“提交”按钮发起第二次请求,action加上name传值相当于访问127.0.0.1:8000/learn/add/?a=2&b=2,得到想要的结果。
Demo2
# urls.py
url(r'^index/$',views.index,name='index'),
# views.py
def index(request):
#return HttpResponse(u"欢迎访问 我的Django学习记录")
#return render(request,'add.html')
if request.method == 'POST':
form = AddForm(request.POST) # form 包含提交的数据
if form.is_valid():# 如果提交的数据合法
a = form.cleaned_data['a']
b = form.cleaned_data['b']
return HttpResponse(str(int(a) + int(b)))
else:# 当正常访问时
form = AddForm()
return render(request, 'add.html', {'form': form})
# add.html
<form method='post'>
{% csrf_token %}
{{ form }}
<input type="submit" value="提交">
{{ form }}的格式是默认的格式,可以测试:
<form method='post'>
{% csrf_token %}
<tr><th><label for="id_a">A:</label></th><td><input id="id_a" name="a" type="number" required /></td></tr>
<tr><th><label for="id_b">B:</label></th><td><input id="id_b" name="b" type="number" required /></td></tr>
<input type="submit" value="提交">
</form>
$ python manage.py shell
In [1]: from learn.forms import AddForm
In [2]: f = AddForm()
In [3]: print f
<tr><th><label for="id_a">A:</label></th><td><input id="id_a" name="a" type="number" required /></td></tr>
<tr><th><label for="id_b">B:</label></th><td><input id="id_b" name="b" type="number" required /></td></tr>
In [4]: print f.as_ul()
<li><label for="id_a">A:</label> <input id="id_a" name="a" type="number" required /></li>
<li><label for="id_b">B:</label> <input id="id_b" name="b" type="number" required /></li>
In [5]: print f.as_p()
<p><label for="id_a">A:</label> <input id="id_a" name="a" type="number" required /></p>
<p><label for="id_b">B:</label> <input id="id_b" name="b" type="number" required /></p>
In [7]: print f['a']
<input id="id_a" name="a" type="number" required />
Demo3
ajax实现再次请求服务器,不刷新浏览器
# learn/urls.py
url(r'^index/$',views.index,name='index'),
# learn/views.py
def index(request):
return render(request,'add.html')
# learn/templates/add.html
<!DOCTYPE html>
<html>
<head>
<title>我的Django学习记录</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"> </script>
</head>
<body>
<p>请输入两个数字</p>
<form action="{% url 'learn:add' %}" method="get">
a: <input type="text" id="a" name="a"> <br>
b: <input type="text" id="b" name="b"> <br>
<p>result: <span id='result'></span></p>
<button type="button" id='sum'>提交</button>
</form>
<script>
$(document).ready(function(){
$("#sum").click(function(){
var a = $("#a").val();
var b = $("#b").val();
$.get("{% url 'learn:add' %}",{'a':a,'b':b}, function(ret){
$('#result').html(ret)
})
});
});
</script>
<br><br>
<input type="button" value="first button" onclick="alert('hello');">
</form>
</body>
</html>
# learn/urls.py
url(r'^add/$',views.add,name="add"),
# learn/views.py
def add(request):
a = request.GET['a']
b = request.GET['b']
c = int(a)+int(b)
return HttpResponse(str(c))