Django的form组件应用登陆或者注册
Posted alone-tree
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django的form组件应用登陆或者注册相关的知识,希望对你有一定的参考价值。
from django import forms from django.forms import Form from django.forms import widgets from django.forms import fields from django.core.exceptions import ValidationError from blog.models import * class RegisterForm(Form): # 自定义form组件 username = fields.CharField( label=‘用户名‘, widget=widgets.TextInput(attrs={‘class‘: ‘form-control‘, ‘placeholder‘: "请输入用户名"}), required=True, max_length=32, strip=True, error_messages={ ‘required‘:‘用户名不能为空‘, ‘max_length‘:‘用户名最大长度为32‘, } ) email = fields.EmailField( label=‘邮箱‘, widget=widgets.TextInput(attrs={‘class‘: ‘form-control‘, ‘placeholder‘: "请输入邮箱"}), required=True, error_messages={ ‘required‘: ‘邮箱不能为空‘, ‘invalid‘: ‘邮箱格式错误‘, } ) password = forms.RegexField( label=‘密码‘, # regex=‘^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[[email protected]#$\%^&*()])[[email protected]#$\%^&*()]{8,32}$‘, # regex=‘^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32}$‘, regex=‘^[0-9A-Za-z]{6,32}$‘, min_length=6, max_length=32, required=True, widget=widgets.PasswordInput(attrs={‘class‘: ‘form-control‘, ‘placeholder‘: "请输入密码"}), error_messages={‘required‘: ‘密码不能为空.‘, ‘invalid‘: ‘密码必须包含数字,字母、特殊字符‘, ‘min_length‘: ‘密码长度不能小于6个字符‘, ‘max_length‘: ‘密码长度不能大于32个字符‘, } ) confirm_pwd = fields.CharField( label=‘确认密码‘, widget=widgets.PasswordInput(attrs={‘class‘: ‘form-control‘, ‘placeholder‘: "请再次输入密码"}), required=True, min_length=6, max_length=32, error_messages={‘required‘: ‘密码不能为空.‘, ‘min_length‘: "密码长度不能小于6个字符", ‘max_length‘: "密码长度不能大于32个字符"} ) # check_code = fields.CharField( # widget=widgets.TextInput(attrs={‘class‘: ‘form-control‘, ‘placeholder‘: "请输入验证码"}), # required=True, # error_messages={‘required‘: ‘验证码不能为空.‘, } # ) def clean_username(self): v = self.cleaned_data.get(‘username‘) if UserInfo.objects.filter(username=v).count(): raise ValidationError(‘用户名已存在‘) else: return v def clean_email(self): v = self.cleaned_data.get(‘email‘) if UserInfo.objects.filter(email=v).count(): raise ValidationError(‘邮箱已存在‘) else: return v def clean(self): v1 = self.cleaned_data.get(‘password‘) v2 = self.cleaned_data.get(‘confirm_pwd‘) if v1 == v2: pass else: raise ValidationError(‘密码输入不一致‘)
以上是关于Django的form组件应用登陆或者注册的主要内容,如果未能解决你的问题,请参考以下文章