|
|
@ -1,13 +1,18 @@ |
|
|
|
from django import forms |
|
|
|
from django import forms |
|
|
|
from django.core.exceptions import ValidationError |
|
|
|
from django.core.exceptions import ValidationError |
|
|
|
from .models import * |
|
|
|
from .models import * |
|
|
|
|
|
|
|
import re |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def age_validate(age): |
|
|
|
def age_validate(age): |
|
|
|
if age <= 0: |
|
|
|
if age <= 0 or age > 150: |
|
|
|
raise ValidationError('年龄错误') |
|
|
|
raise ValidationError('年龄错误') |
|
|
|
elif age > 150: |
|
|
|
|
|
|
|
raise ValidationError('老妖精') |
|
|
|
|
|
|
|
|
|
|
|
def mobile_validate(mobile): |
|
|
|
|
|
|
|
mobile_re = re.compile(r'^1[3-9]]\d{9}$') |
|
|
|
|
|
|
|
if not mobile_re.match(mobile): |
|
|
|
|
|
|
|
raise ValidationError('手机号格式错误') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 用户信息表单 |
|
|
|
# 用户信息表单 |
|
|
@ -19,11 +24,13 @@ class UserInfoForm(forms.Form): |
|
|
|
'invalid': '请输入正确用户名'}) |
|
|
|
'invalid': '请输入正确用户名'}) |
|
|
|
password = forms.CharField(label='密码', min_length=6, max_length=10, |
|
|
|
password = forms.CharField(label='密码', min_length=6, max_length=10, |
|
|
|
widget=forms.widgets.PasswordInput(attrs={'class': 'password'}, render_value=True), |
|
|
|
widget=forms.widgets.PasswordInput(attrs={'class': 'password'}, render_value=True), |
|
|
|
help_text='密码长度为6-10个字符') |
|
|
|
help_text='密码长度为6-10个字符', error_messages={'required': '密码不能为空'}) |
|
|
|
age = forms.IntegerField(label='年龄', initial=1, min_value=1, max_value=150, validators=[age_validate], |
|
|
|
age = forms.IntegerField(label='年龄', initial=1, min_value=1, max_value=150, validators=[age_validate], |
|
|
|
error_messages={'required': '年龄不能为空'}) |
|
|
|
error_messages={'required': '年龄不能为空'}) |
|
|
|
mobile = forms.CharField(label='手机号码', min_length=11, max_length=11) |
|
|
|
mobile = forms.CharField(label='手机号码', validators=[mobile_validate], |
|
|
|
status = forms.ChoiceField(label='状态', choices=STATUS, widget=forms.widgets.Select, |
|
|
|
error_messages={'required': '手机号不能为空'}) |
|
|
|
|
|
|
|
# status = forms.ChoiceField(label='状态', choices=STATUS, widget=forms.widgets.Select, |
|
|
|
|
|
|
|
# error_messages={'required': '状态不能为空'}) |
|
|
|
|
|
|
|
status = forms.ModelChoiceField(label='状态', queryset=UserStatus.objects.all(), empty_label='请选择', |
|
|
|
error_messages={'required': '状态不能为空'}) |
|
|
|
error_messages={'required': '状态不能为空'}) |
|
|
|
# status = forms.ModelChoiceField(queryset=UserStatus.objects.all(), empty_label='请选择') |
|
|
|
|
|
|
|
createdate = forms.DateField(label='创建日期', required=False, input_formats=["%Y-%m-%d"]) |
|
|
|
createdate = forms.DateField(label='创建日期', required=False, input_formats=["%Y-%m-%d"]) |
|
|
|