Form
django中的form一般有两种功能
1.输入html
2.验证用户输入
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import re 4 from django import forms 5 from django.core.exceptions import ValidationError 6 7 8 def mobile_validate(value): 9 mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$') 10 if not mobile_re.match(value): 11 raise ValidationError('手机号码格式错误') 12 13 14 class PublishForm(forms.Form): 15 16 user_type_choice = ( 17 (0, u'普通用户'), 18 (1, u'高级用户'), 19 ) 20 21 user_type = forms.IntegerField(widget=forms.widgets.Select(choices=user_type_choice, 22 attrs={'class': "form-control"})) 23 24 title = forms.CharField(max_length=20, 25 min_length=5, 26 error_messages={'required': u'标题不能为空', 27 'min_length': u'标题最少为5个字符', 28 'max_length': u'标题最多为20个字符'}, 29 widget=forms.TextInput(attrs={'class': "form-control", 30 'placeholder': u'标题5-20个字符'})) 31 32 memo = forms.CharField(required=False, 33 max_length=256, 34 widget=forms.widgets.Textarea(attrs={'class': "form-control no-radius", 'placeholder': u'详细描述', 'rows': 3})) 35 36 phone = forms.CharField(validators=[mobile_validate, ], 37 error_messages={'required': u'手机不能为空'}, 38 widget=forms.TextInput(attrs={'class': "form-control", 39 'placeholder': u'手机号码'})) 40 41 email = forms.EmailField(required=False, 42 error_messages={'required': u'邮箱不能为空','invalid': u'邮箱格式错误'}, 43 widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': u'邮箱'})) 44 45 Form