from django.shortcuts import render_to_response
from django import forms
from django.http import HttpResponse
class UserForm(forms.Form):
name = forms.CharField()
def register(req):
if req.method == 'POST':
form_object = UserForm(req.POST)
if form_object.is_valid():
print form_object.cleaned_data
return HttpResponse('ok')
else:
form_object = UserForm()
return render_to_response('index.html',{'form':form_object})
templates/index.html
<html>
<head>
</head>
<body>
<form method='post'>
`form`
<button type='sumbit'>ok</button>
</form>
</body>
</html>
Name:输入内容 ok按钮
说下流程是怎么走的
第一次访问他的时候是get方法
也就会走到这个逻辑
form_object = UserForm()
return render_to_response('index.html',{'form':form_object})实例化UserForm得到的,等于这个类相当于content
当点击ok按钮后,判断是否为post方法
form_object = UserForm(req.POST) 绑定动作必须写
if form_object.is_valid() 判断字段是否存在
print form_object.cleaned_data 获取提交的数据,在终端打出来
return HttpResponse('ok') 在web页面上返回ok
转载于:https://blog.51cto.com/4249964/1600796