1.在setting.py文件中修改节点,注释掉其中一行
MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', )
2.在urls.py中添加对应的url
url(r'^app1/register/$', app1.views.register),
3.定义form类对象
class UserForm(forms.Form): name=forms.CharField()
4.views.py中添加对应url的方法
def register(req): if req.method == 'POST': form = UserForm(req.POST) #创建 form对象 if form.is_valid():#验证是否正确 print form.cleaned_data #打印界面对应取值
return HttpResponse('ok') else: form = UserForm() return render_to_response('register.html',{'form':form})
5.在templates中添加对应模板文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title></title> </head> <body> <form method="POST"> {{form.as_p}}#as_p 分行显示 默认显示在一行
<input type="submit" value="ok" /> </form> </body> </html>