class Meta: model = UserInfo fields = '__all__' widgets = { 'username': widgets.TextInput(attrs={'class': 'form-control input-lg'}), 'password': widgets.PasswordInput(attrs={'class': 'form-control input-lg'}), 'name': widgets.TextInput(attrs={'class': 'form-control input-lg'}), 'email': widgets.EmailInput(attrs={'class': 'form-control input-lg'}), 'types': widgets.NumberInput(attrs={'class': 'form-control input-lg'}), 'database_id': widgets.NumberInput(attrs={'class': 'form-control input-lg'}), 'roles': widgets.Select(attrs={'class': 'form-control input-lg'}), } labels = { 'username': '******', 'password': '******', 'name': '姓名', 'email': '邮箱', 'types': '用户类型', 'database_id': '数据库编号', 'roles': '角色', }
class LoginForm(Form): pTel = fields.CharField( label="手机", widget=widgets.TextInput(attrs={"class": "form-control"}), # form 组件中用正则判断是否符合条件 validators=[RegexValidator(r'^[0-9]+$', '请输入数字'), RegexValidator(r'^1[35789]\d{9}$', '请输入正确手机号')], error_messages={ "required": "手机号码不能为空", } ) # 调用form组件内部方法创建一个长度不大于16且不小于8的密码文本框 pPassword = fields.CharField( label="密码", max_length=16, min_length=6, # 定义为密码文本,render_value设置为验证不通过时不把密码刷新掉 widget=widgets.PasswordInput(attrs={"class": "form-control"}, render_value=True), error_messages={ "min_length": "密码不能少于6位数", "required": "密码不能为空" } ) captcha = CaptchaField( label='验证码', error_messages={ 'invalid': '验证码输入错误', } )
class ServerUserLine(admin.StackedInline): model = ServerUserPass extra = 1 readonly_fields = ['user_name'] fields = ( 'user_name', 'user_passwd', ) # 重写 字段类型 的 widget,使用PasswordInput 让前端输入密码为密文 formfield_overrides = { models.CharField: { 'widget': widgets.PasswordInput( attrs={ "style": "width:50%;", "class": "form-control", "render_value": True, "placeholder": "请输入密码" }) }, } def has_delete_permission(self, request, obj=None): """隐藏删除按钮""" return False
class XassrUserCreationForm(UserCreationForm): username = forms.CharField(label=u"ユーザーID", help_text=u"空白を含めない英数字、記号で入力してください。") screen_name = forms.CharField(label=u"表示名", required=True) password1 = forms.CharField(label=u"パスワード", widget=widgets.PasswordInput()) class Meta(UserCreationForm.Meta): fields = ('username', 'screen_name', 'email', 'password1', 'password2') def save(self, commit=True, token_generator=default_token_generator): user = super(XassrUserCreationForm, self).save(commit=False) if commit: user.screen_name = self.cleaned_data['screen_name'] user.token = token_generator.make_token(user) user.save() return user def clean_email(self): value = self.cleaned_data['email'] count = User.objects.filter(email=value).count() if count > 0: raise forms.ValidationError(u"このメールアドレスは既に使われています。") else: return value def clean_username(self): value = self.cleaned_data['username'] if ' ' in value: raise forms.ValidationError(u"ユーザーIDは空白を含めない英数字、記号で入力してください。") else: return value
class AddForm(forms.Form): ip = fields.CharField( required=True, error_messages={'required': "IP不能为空"}, widget=widgets.TextInput(attrs={"class": "form-control", "name": "ip", "placeholder": "1.1.1.1"}) ) port = fields.CharField( required=True, error_messages={"required": "Port不能为空"}, widget=widgets.TextInput(attrs={"class": "form-control", "name": "port", "placeholder": "22"}) ) hostuser = fields.CharField( required=True, error_messages={"required": "主机用户不能为空"}, widget=widgets.TextInput(attrs={"class": "form-control", "name": "hostuser", "placeholder": "root"}) ) hostpass = fields.CharField( required=True, error_messages={"required": "主机密码不能为空"}, widget=widgets.PasswordInput(attrs={"class": "form-control", "name": "hostpass", "placeholder": "111111"}) ) bussiness_id = fields.IntegerField( required=True, widget=widgets.Select(attrs={"class": "form-control"}), ) def __init__(self, *args, **kwargs): super(AddForm, self).__init__(*args, **kwargs) self.fields['bussiness_id'].widget.choices = Bussiness.objects.values_list('id', 'bussline')
class ArtsUserLoginForm(forms.Form): username = forms.CharField( label="用户名", required=True, min_length=1, max_length=10, widget=widgets.TextInput(attrs={ "class": "form-control", "placeholder": "请输入用户名,长度为1 ~ 10", }), error_messages={ "required": "对不起,输入的用户名不能为空", "min_length": "不行, 长度小于1", "max_length": "sorry, 长度大于10", }) password = forms.CharField( label="密码", required=True, min_length=1, max_length=10, widget=widgets.PasswordInput(attrs={ "class": "form-control", "placeholder": "请输入密码, 长度为1~10", }), error_messages={ "required": "密码不能为空", "min_length": "不行, 长度小于1", "max_length": "sorry, 长度大于10", }) captcha = CaptchaField(label='验证码')
class UserInfoForm(forms.Form): user = fields.CharField( required=False, widget=widgets.Textarea(attrs={'class':'c1'}) ) pwd = fields.CharField( max_length=12, widget=widgets.PasswordInput(attrs={'class':'c1'}) ) user_type=fields.ChoiceField( # choices=[(1,'普通用户'),(2,'超级用户'),], # choices=models.UserType.objects.values_list('id','name'), choices=[], widget=widgets.Select, ) user_type2 = fields.CharField( widget=widgets.Select(choices=[]) ) user_type3 = ModelChoiceField( empty_label='请选择用户', queryset=models.UserType.objects.all(), to_field_name='name', ) def __init__(self,*args,**kwargs): super(UserInfoForm,self).__init__(*args,**kwargs) self.fields['user_type'].choices = models.UserType.objects.values_list('id', 'name') self.fields['user_type2'].widget.choices = models.UserType.objects.values_list('id', 'name')
class LoginForm(forms.Form): username = fields.CharField(required=True, widget=widgets.TextInput(attrs={ 'class': 'form-control', 'placeholder': '用户名' }), label='用户名', max_length=12, min_length=6, error_messages={'required': '用户名不为空'}) password = fields.CharField( required=True, widget=widgets.PasswordInput(attrs={ 'class': 'form-control', 'placeholder': '请输入密码' }), min_length=6, max_length=12, label='密码', error_messages={ 'required': '密码不能为空', 'min_length': '用户名最少为6个字符', 'max_length': '用户名不超过12个字符', }, ) def clean(self): username = self.cleaned_data.get('username') pwd = self.cleaned_data.get('password') user = User.objects.filter(username=username).first() if username and pwd: if not user: raise ValidationError('用户不存在!') if not user.check_password(pwd): raise ValidationError('用户或者密码错误!')
class UserProfileForm(Form): """用户认证Form""" username = fields.CharField( error_messages={ "required": "用户名不能为空", }, widget=widgets.TextInput( attrs={ "class": "form-control", "name": "username", } ) ) password = fields.CharField( error_messages={ "required": "密码不能为空", }, widget=widgets.PasswordInput( attrs={ "class": "form-control", "name": "password", } ) ) def clean_password(self): """对密码进行加密""" encrypt_password = str_encrypt(self.cleaned_data['password']) return encrypt_password
class Denglu(forms.Form): use = forms.CharField(label="用户名", error_messages={"required": "不能为空"}, widget=widgets.TextInput(attrs={ "class": "form-control", 'placeholder': "请输入用户名" })) pwd = forms.CharField(label="密码", error_messages={"required": "不能为空"}, widget=widgets.PasswordInput(attrs={ "class": "form-control", 'placeholder': "请输入密码" })) chex = forms.CharField(label="记住密码", required=False, widget=widgets.CheckboxInput()) def clean(self): name = self.cleaned_data.get("use") pwd = self.cleaned_data.get("pwd") # print(self.cleaned_data) if pwd != None: # print(name,pwd) user = auth.authenticate(username=name, password=pwd) # print(name,type(pwd),user) if not user: raise ValidationError("账号或密码错误") else: return self.cleaned_data
class Login(forms.Form): error_msg = {'required': '该字段不能为空'} username = forms.CharField( min_length=6, label='用户名', error_messages=error_msg, ) password = forms.CharField( min_length=6, label='密码', error_messages=error_msg, widget=widgets.PasswordInput() # 隐藏密码 ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for filed in self.fields.values(): filed.widget.attrs.update({'class': 'form-control'}) # 判断用户名密码 def clean(self): username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') user_obj = auth.authenticate(username=username, password=password) if user_obj: return self.cleaned_data else: self.add_error('password', ValidationError('用户名或密码错误!'))
class FM(forms.Form): # 通过form生成HTML页面 user = fields.CharField( error_messages={'required': '用户名不能为空'}, widget=widgets.Textarea(attrs={'class': 'c1'}), label='用户名', ) pwd = fields.CharField(max_length=32, min_length=6, error_messages={ 'required': '密码不能为空', 'min_length': '密码最少为6个字符', 'max_length': '密码最多为32个字符' }, widget=widgets.PasswordInput(attrs={'class': 'c2'})) email = fields.EmailField(error_messages={ 'required': '邮箱不能为空.', 'invalid': "邮箱格式错误" }) f = fields.FileField(allow_empty_file=True) city1 = fields.ChoiceField(choices=[(0, '上海'), (1, '广州'), (2, '东莞')]) city2 = fields.MultipleChoiceField(choices=[(0, '上海'), (1, '广州'), (2, '东莞')])
class Meta: # 与models建立依赖关系 model = UserInfo # 字段,__all__ 表示列出所有字段 # fields = "__all__" fields = ['username', 'password', 'email'] # 排除的字段 exclude = None # 单独设置每个错误; # error_messages 全局设置, 所以加s error_messages = { "username": { 'required': '用户名不能为空', }, "password": { 'required': '密码不能为空', }, # "r_password": {'r_required': '确认密码不能为空'}, "email": { 'required': '邮件不能为空', }, } labels = { "username": "******", "password": "******", # "r_password": "******", "email": "邮件", } widgets = { "password": wid.PasswordInput(), # "r_password": wid.PasswordInput(), }
class UserProfileForm(forms.Form): email = forms.EmailField() password = forms.CharField(max_length=100, widget=widgets.PasswordInput()) first_name = forms.CharField(max_length=100) last_name = forms.CharField(max_length=100) is_admin = forms.BooleanField(initial=False, required=False) is_active = forms.BooleanField(initial=True, required=False)
class loginForm(forms.Form): username = fields.CharField( required=True, widget=widgets.TextInput(attrs={'class': "form-control",'placeholder': 'username'}), min_length=6, max_length=12, strip=True, error_messages={'required': 'username cannot be empty',} ) pwd = fields.CharField( widget=widgets.PasswordInput(attrs={'class': "form-control",'placeholder': 'passward'}), required=True, min_length=6, max_length=12, strip=True, error_messages={'required': 'password cannot be empty',} ) def clean(self): username = self.cleaned_data.get('username') pwd = self.cleaned_data.get('pwd') user = models.user_info.objects.filter(username=username) # if username and pwd: if not user.exists(): raise forms.ValidationError('username does not exist!') elif pwd != user[0].password: raise forms.ValidationError('wrong password')
class ArtsUserLoginForm(forms.Form): username = forms.CharField( label="用户名", required=True, min_length=3, max_length=50, widget=widgets.TextInput(attrs={ "class": "form-control", "placeholder": "请输入用户名, 长度为3~50", }), error_messages={ "required": "对不起,用户名不能为空!", "min_length": "不行,长度小于3", "max_length": "sorry, 长度太长,大于50", }) password = forms.CharField( label="密码", required=True, min_length=6, max_length=20, widget=widgets.PasswordInput(attrs={ "class": "form-control", "placeholder": "请输入密码, 长度为6~20", }), error_messages={ "required": "对不起,密码不能为空!", "min_length": "不行,长度小于6", "max_length": "sorry, 长度太长,大于20", })
class LoginForm(Form): username = fields.CharField( required=True, label='username', max_length=100, min_length=5, error_messages={ "required": "用户名不能为空", "label": "label标签绑定出错", "max_length": "最大长度超过100", "min_length": "用户名小于5位", }, widget=widgets.TextInput(attrs={ "placeholder": "请输入用户名", "class": "form-control" }) #设置属性 ) password = fields.CharField( required=True, label='password', max_length=100, min_length=5, error_messages={ "required": "用户名不能为空", "label": "label标签绑定出错", "max_length": "最大长度超过100", "min_length": "用户名小于5位", }, widget=widgets.PasswordInput(attrs={ "placeholder": "请输入密码", "class": "form-control" }))
class TestForm(forms.Form): n1 = fields.CharField(widget=widgets.PasswordInput()) n2 = fields.CharField(widget=widgets.Textarea()) n3 = fields.CharField(widget=widgets.Select(choices=[ (1, "上海"), (2, "北京"), (3, "广州"), ])) n4 = fields.ChoiceField(choices=[(1, "上海"), (2, "北京"), (3, "广州")], widget=widgets.SelectMultiple()) n5 = fields.IntegerField(widget=widgets.RadioSelect(choices=[ (1, '上海'), (2, '北京'), ])) n6 = fields.CharField(widget=widgets.CheckboxInput()) n7 = fields.ChoiceField(choices=[ (1, '上海'), (2, '北京'), ], widget=widgets.CheckboxSelectMultiple()) n8 = fields.FileField()
class AuthForm(forms.Form): email = AdvEmailField(max_length=75, label=_('Email')) password = fields.CharField(max_length=30, label=_('Password'), widget=widgets.PasswordInput(render_value=True)) change_password_link = getattr(settings, 'CHANGE_PASSWORD_LINK_ON_WRONG_LOGIN', False) def get_user(self): return self.user def clean(self): if 'email' in self.cleaned_data and 'password' in self.cleaned_data: self.user = authenticate(username=self.cleaned_data['email'], password=self.cleaned_data['password']) if self.user is None: change_url = None if self.change_password_link: try: change_url = reverse('change_password') except NoReverseMatch: pass if change_url: raise forms.ValidationError(mark_safe( _('Wrong email or password. If you think you\'ve forgotten your credentials, you can <a href="%(url)s">change your password</a>.') % {'url': change_url} )) else: raise forms.ValidationError(_('Wrong email or password.')) elif not self.user.is_active: raise forms.ValidationError(_('Your account is blocked.')) else: return self.cleaned_data
class LoginForm(BootstrapForm): username = forms.CharField(label='用户名', max_length=50, error_messages={"required": "用户名不能为空!"}, widget=widgets.TextInput(attrs={'placeholder': '请输入用户名/邮箱/电话'})) password = forms.CharField(label='密码', widget=widgets.PasswordInput(attrs={'placeholder': '请输入密码'}), error_messages={"required": "密码不能为空!"}) def clean_username(self): username = self.cleaned_data.get('username') if email_check(username): filter_result = UserInfo.objects.filter(email__exact=username) if not filter_result: raise forms.ValidationError("此邮箱不存在") return username if phone_check(username): filter_result = UserInfo.objects.filter(tel__exact=username) if not filter_result: raise forms.ValidationError("此号码不存在") else: filter_result = UserInfo.objects.filter(username__exact=username) if not filter_result: raise forms.ValidationError("此账号不存在") return username
class UserProfileWithTokenSerializer(UserProfileSerializer): username = serializers.WritableField(source='user.username') email = serializers.WritableField(source='user.email') website = serializers.WritableField(source='home_page', required=False) gravatar = serializers.Field(source='gravatar') password = serializers.WritableField( source='user.password', widget=widgets.PasswordInput(), required=False) user = serializers.HyperlinkedRelatedField( view_name='user-detail', lookup_field='username', read_only=True) api_token = serializers.SerializerMethodField('get_api_token') temp_token = serializers.SerializerMethodField('get_temp_token') class Meta: model = UserProfile fields = ('url', 'username', 'name', 'password', 'email', 'city', 'country', 'organization', 'website', 'twitter', 'gravatar', 'require_auth', 'user', 'api_token', 'temp_token') lookup_field = 'user' def get_api_token(self, object): return object.user.auth_token.key def get_temp_token(self, object): token, created = TempToken.objects.get_or_create(user=object.user) return token.key
class FM(forms.Form): # 字段本身只做验证 user = fields.CharField( error_messages={'required': '用户名不能为空.'}, widget=widgets.Textarea(attrs={'class': 'c1'}), label="用户名", ) pwd = fields.CharField(max_length=12, min_length=6, error_messages={ 'required': '密码不能为空.', 'min_length': '密码长度不能小于6', "max_length": '密码长度不能大于12' }, widget=widgets.PasswordInput(attrs={'class': 'c2'})) email = fields.EmailField(error_messages={ 'required': '邮箱不能为空.', 'invalid': "邮箱格式错误" }) f = fields.FileField() # p = fields.FilePathField(path='app01') city1 = fields.ChoiceField(choices=[(0, '上海'), (1, '广州'), (2, '东莞')]) city2 = fields.MultipleChoiceField(choices=[(0, '上海'), (1, '广州'), (2, '东莞')])
class LoginForm(forms.Form): username = forms.CharField(max_length=20, label='Username', widget=widgets.TextInput( attrs={ 'class': 'form-control', 'placeholder': 'Username', 'id': 'inputUsername' })) password = forms.CharField(max_length=50, label='Password', widget=widgets.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': 'Password', 'id': 'inputPassword' })) def clean_username(self): username = self.cleaned_data.get('username') if not User.objects.filter(username__exact=username): raise forms.ValidationError( "This username does not exist. Please register first.") return username
class ChangepassForm(forms.Form): yuapassword = fields.CharField(min_length=6, max_length=16, error_messages={ 'required': '不能为空', 'max_length': '密码输入太长了', 'min_length': '密码输入太短了' }, widget=widgets.PasswordInput(attrs={'class': 'form-control'})) password = fields.CharField(min_length=6, max_length=16, error_messages={ 'required': '不能为空', 'max_length': '密码输入太长了', 'min_length': '密码输入太短了' }, widget=widgets.PasswordInput(attrs={'class': 'form-control'})) commpassword = fields.CharField(min_length=6, max_length=16, error_messages={ 'required': '不能为空', 'max_length': '密码输入太长了', 'min_length': '密码输入太短了' }, widget=widgets.PasswordInput(attrs={'class': 'form-control'}))
class StudentAddModelForm(StarkModelForm): confirm_password = forms.CharField(label='确认密码', widget=widgets.PasswordInput(attrs={'class': 'form-control'}), error_messages={'required': "确认密码不能为空!"}) class Meta: model = models.Student fields = ['name', 'password', 'confirm_password', 'stu_name', 'stu_gender', 'education', 'school', 'course', ] widgets = { 'password': forms.PasswordInput(attrs={'class': 'form-control'}), } def clean_confirm_password(self): if self.cleaned_data.get('password') != self.cleaned_data['confirm_password']: raise ValidationError('两次密码不一致') return self.cleaned_data['confirm_password'] def clean(self): if not self.cleaned_data.get('password'): raise ValidationError('请输入密码') raw_password = self.cleaned_data['password'] self.cleaned_data['password'] = raw_password return self.cleaned_data
class TeacherAddModelForm(StarkModelForm): confirm_password = forms.CharField( label='确认密码', widget=widgets.PasswordInput(attrs={'class': 'form-control'}), error_messages={'required': "确认密码不能为空!"}) class Meta: model = models.Teacher fields = [ 'name', 'password', 'confirm_password', 'nickname', 'gender', 'wechat', 'phone', 'course', 'free_time' ] widgets = { 'password': forms.PasswordInput(attrs={'class': 'form-control'}), } def clean_confirm_password(self): if self.cleaned_data.get( 'password') != self.cleaned_data['confirm_password']: raise ValidationError('两次密码不一致') return self.cleaned_data['confirm_password'] def clean(self): if not self.cleaned_data.get('password'): raise ValidationError('请输入密码') raw_password = self.cleaned_data['password'] self.cleaned_data['password'] = raw_password return self.cleaned_data
class LoginForm(forms.Form): username = forms.CharField(max_length=16, widget=widgets.Input( attrs={ 'type': "username", 'class': "form-control", 'id': "exampleInputUsername" })) password = forms.CharField(max_length=16, min_length=6, widget=widgets.PasswordInput( attrs={ 'type': "password", 'class': "form-control", 'id': "exampleInputPassword" })) def clean_password(self): password = self.cleaned_data['password'] password_length = len(password) if password_length < 6: raise forms.ValidationError("Not enough digits for password!") elif password_length > 16: raise forms.ValidationError("Too much digits for password!") return password
class FM(forms.Form): user = fields.CharField(error_messages={'required': '用户名不能为空'}, widget=widgets.Input(attrs={'class': 'c1'}), label="用户名") pwd = fields.CharField(max_length=12, min_length=6, error_messages={ 'required': '密码不能为空', 'max_length': '密码长度不能大于12位', 'min_length': '密码长度不能小于6位' }, widget=widgets.PasswordInput(attrs={'class': 'c2'}), label="密码") email = fields.EmailField(error_messages={ 'required': '邮箱不能为空', 'invalid': '邮箱格式错误' }, label="邮箱") f = fields.FileField(allow_empty_file=False) p = fields.FilePathField(path='app01') # 列出app01目录下所有文件 city1 = fields.ChoiceField(choices=[(0, '北京'), (1, '吉林'), (2, '银川')]) city2 = fields.MultipleChoiceField(choices=[(0, '广东'), (1, '深圳'), (2, '东莞')])
class Meta: model = DataBases fields = "__all__" widgets = { # render_value 用来回显 密码 ,否则设置为passwordinput的话 修改的时候就为空了 "db_passwd": Fwidgets.PasswordInput(render_value=True), }
class Meta: model = User fields = ("password", ) widgets = { 'password': Fwidgets.PasswordInput(attrs={'class': 'form-control'}), }