django 学习-12 Django表单 初步
1、先创建项目和应用
django.admin.py startproject cs
cd cs
django.admin.py startapp blog
2、vim setting.py
加应用,把数据库加为sqlite3
vim urls.py
url(r'^blog/register/$','blog.views.register'),
vim views.py
from django import forms
from django.http import HttpResponse
from django.shortcuts import render_to_response
class UserForm(forms.Form):
name = forms.CharField()
def register(req):
if req.method == 'POST': method 默认是get的方式
form = UserForm(req.POST) 这是一个绑定动作
if form.is_valid(): 对字段进行最基本的验证
print form.cleaned_data
return HttpResponse('ok')
else :
form = UserForm()
return render_to_response('register.html',{'form':form})
mkdir blog/templates
vim blog/templates/register.html
<!DOCTYPE>
<html>
<head>
<title></title>
</head>
<body>
<form method = "post">
{{form}}
<input type="submit" value="ok" />
</form>
</body>
</html>
3、python manage.py runserver
然后网页:127.0.0.1:8000/blog/register就OK了