from django.forms import Form
from django.forms import fields
from django.forms import widgets
class LoginForm(Form):
username = fields.CharField(
required=True,
error_messages={
"required": "用户名不能为空!"
},
widget=widgets.TextInput(attrs={
"placeholder": "Username"
}))
password = fields.CharField(
required=True,
error_messages={
"required": "密码不能为空!"
},
widget=widgets.PasswordInput(attrs={
"placeholder": "Password"
}))
<p>用户名:</p>
<p>密码:</p>
def _clean_fields(self):
for name, field in self.fields.items():
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
if field.disabled:
value = self.get_initial_for_field(field, name)
else:
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
try:
if isinstance(field, FileField):
initial = self.get_initial_for_field(field, name)
value = field.clean(value, initial)
else:
value = field.clean(value)
self.cleaned_data[name] = value
# ##############################
if hasattr(self, 'clean_%s' % name):
value = getattr(self, 'clean_%s' % name)()
self.cleaned_data[name] = value
# ##############################
except ValidationError as e:
self.add_error(name, e)
def clean_username(self):
"""钩子函数"""
user = self.cleaned_data["username"]
is_exist = models.User.objects.filter(username=user).count()
if not is_exist:
raise ValidationError("用户名不存在!")
return user
def clean(self):
"""全局校验"""
if self.cleaned_data.get("password") != self.cleaned_data.get("repeat_password"):
self.add_error("repeat_password", "两次密码不一致!")
return self.cleaned_data
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["目标字段"].choices = [(1,111),(2,222)...]或ORM的values_list返回值
from django.forms.models import ModelChoiceField
cls = ModelChoiceField(
queryset=models.Classes.objects.all(),
widget=widgets.Select()
)
class RegisterForm(Form):
"""
注册表单
"""
email = fields.EmailField(
required=True,
error_messages={
"required": "邮箱不能为空!",
"invalid": "邮箱格式错误!",
},
widget=widgets.TextInput(attrs={
"class": "form-control",
"placeholder": "请输入您的邮箱地址",
"id": "email",
}),
)
username = fields.CharField(
required=True,
max_length=32,
min_length=3,
error_messages={
"required": "用户名不能为空!",
"max_length": "用户名过长!",
"min_length": "用户名过短!"
},
widget=widgets.TextInput(attrs={
"class": "form-control",
"placeholder": "请输入用户名,必须是字母、数字、下划线、'-'组成",
"id": "username",
}),
)
password = fields.CharField(
required=True,
max_length=32,
min_length=6,
error_messages={
"required": "密码不能为空!",
"max_length": "密码过长!",
"min_length": "密码过短!"
},
widget=widgets.PasswordInput(attrs={
"class": "form-control",
"placeholder": "请输入密码",
"id": "password",
}),
)
repeat_password = fields.CharField(
required=True,
max_length=32,
min_length=6,
error_messages={
"required": "密码不能为空!",
"max_length": "密码过长!",
"min_length": "密码过短!"
},
widget=widgets.PasswordInput(attrs={
"class": "form-control",
"placeholder": "请再次输入密码",
"id": "repeat_password",
})
)
nickname = fields.CharField(
required=True,
min_length=2,
error_messages={
"required": "昵称不能为空!",
"min_length": "昵称过短!"
},
widget=widgets.TextInput(attrs={
"class": "form-control",
"placeholder": "请输入昵称",
"id": "nickname",
}),
)
phone = fields.CharField(
required=True,
error_messages={
"required": "手机号不能为空!",
},
widget=widgets.TextInput(attrs={
"class": "form-control",
"placeholder": "请输入手机号",
"id": "phone",
}),
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
from app01.models import User
self.user = User
def clean(self):
"""全局校验"""
# 校验两次密码是否一致
if not self.cleaned_data.get("password") == self.cleaned_data.get("repeat_password"):
self.add_error("repeat_password", "两次密码不一致")
return self.cleaned_data
def clean_email(self):
"""邮箱验证"""
email = self.cleaned_data.get("email")
if self.user.objects.filter(userinfo__email=email):
raise ValidationError("该邮箱已注册!")
else:
return email
def clean_username(self):
"""校验用户名"""
username = self.cleaned_data.get("username")
user = self.user.objects.filter(username=username)
if user:
raise ValidationError("用户名已存在!")
from re import search
if len(search("[\w-]*", username).group()) != len(username):
raise ValidationError("用户名只能是数字、字母、下划线和'-'组成!")
return username
def clean_password(self):
"""校验密码"""
from re import findall
password = self.cleaned_data.get("password")
if findall("\d+", password) and findall("[a-zA-Z]+", password):
return password
raise ValidationError("密码必须包含数字和字母")
def clean_phone(self):
"""校验手机号"""
from re import findall
phone = self.cleaned_data.get("phone")
if phone and len(phone) == 11:
if findall("^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$", phone):
if self.user.objects.filter(userinfo__phone=phone):
raise ValidationError("该手机号已注册!")
else:
return phone
else:
raise ValidationError("请输入正确的手机号!")
else:
raise ValidationError("手机号必须是11位!")