首页 > 代码库 > Django表单
Django表单
1. get方式请求表单
settings.py添加templates路径
TEMPLATES = [ { ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘, ‘DIRS‘: [BASE_DIR+‘/templates‘,], ‘APP_DIRS‘: True, ‘OPTIONS‘: { ‘context_processors‘: [ ‘django.template.context_processors.debug‘, ‘django.template.context_processors.request‘, ‘django.contrib.auth.context_processors.auth‘, ‘django.contrib.messages.context_processors.messages‘, ], }, }, ]
templates/search_form.html
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>search_form</title> </head> <body> <form action="/search/" method="get"> <input type="text" name="q"> <input type="submit" value="Search"> </form> </body> </html>
新建search.py文件,并写入:
# coding=utf8 from django.http import HttpResponse from django.shortcuts import render_to_response # 表单 def search_form(request): return render_to_response(‘search_form.html‘) # 接收请求数据 def search(request): request.encoding=‘utf-8‘ if ‘q‘ in request.GET: message = ‘你搜索的内容为:‘ + str(request.GET[‘q‘]) else: message = ‘你提交了空表单‘ return HttpResponse(message)
urls.py
from django.conf.urls import url from django.contrib import admin from HelloWorld import search urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^search-form/$‘, search.search_form), url(r‘^search/$‘, search.search), ]
django form类
Form表单的功能
自动生成HTML表单元素
检查表单数据的合法性
如果验证错误,重新显示表单(数据不会重置)
数据类型转换(字符类型的数据转换成相应的Python类型)
新建一个form.py,写入:
from django import forms class ContactForm(forms.Form): subject = forms.CharField() email = forms.EmailField(required=False) message = forms.CharField()
在命令行里测试form类
>>> print f.as_ul()
<li><label for="id_subject">Subject:</label> <input type="text" name="subject" id="id_subject" /></li>
<li><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></li>
<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>
>>> print f.as_p()
<p><label for="id_subject">Subject:</label> <input type="text" name="subject" id="id_subject" /></p>
<p><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></p>
<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>
>>> print f[‘subject‘]
<input type="text" name="subject" id="id_subject" />
>>> print f[‘message‘]
<input type="text" name="message" id="id_message" />
>>> f = ContactForm({‘subject‘: ‘Hello‘, ‘email‘: ‘adrian@example.com‘, ‘message‘: ‘Nice site!‘})
// 一旦你对一个Form实体赋值,你就得到了一个绑定form:
>>> f.is_bound
True
>>> f.is_valid()
True
>>> f = ContactForm({‘subject‘: ‘Hello‘, ‘message‘: ‘Nice site!‘})
>>> f.is_valid()
True
>>> f = ContactForm({‘subject‘: ‘Hello‘})
>>> f.is_valid()
False
>>> f = ContactForm({‘subject‘: ‘Hello‘, ‘message‘: ‘‘})
>>> f.is_valid()
False
>>> f = ContactForm({‘subject‘: ‘Hello‘, ‘message‘: ‘‘})
>>> f[‘message‘].errors
[u‘This field is required.‘]
>>> f[‘subject‘].errors
[]
>>> f[‘email‘].errors
[]
>>> f = ContactForm({‘subject‘: ‘Hello‘, ‘message‘: ‘‘})
>>> f.errors
{‘message‘: [u‘This field is required.‘]}
>>> f = ContactForm({subject‘: Hello, email: adrian@example.com, message: Nice site!})
>>> f.is_valid()
True
>>> f.cleaned_data
{message‘: uNice site!, email: uadrian@example.com, subject: uHello}
如果一个Form实体的数据是合法的,它就会有一个可用的cleaned_data属性, 这是一个包含干净的提交数据的字典。
github地址:
django_form
https://github.com/liuqiuchen/django
Django表单