class Meta: image = forms.CharField(required=True, widget=forms.TextInput(attrs={'class': 'form-control'})) model = Apartments exclude = ['id', 'forsale'] widgets = { 'registration': widgets.TextInput(attrs={'class': 'form-control'}), 'address': widgets.TextInput(attrs={'class': 'form-control'}), 'aptsuite': widgets.TextInput(attrs={'class': 'form-control'}), 'size': widgets.NumberInput(attrs={'class': 'form-control'}), 'rooms': widgets.NumberInput(attrs={'class': 'form-control'}), 'bathrooms': widgets.NumberInput(attrs={'class': 'form-control'}), 'timeofconstruction': widgets.TextInput(attrs={'class': 'form-control'}), 'type': widgets.TextInput(attrs={'class': 'form-control'}), 'owner': widgets.TextInput(attrs={'class': 'form-control'}) }
class Meta: model = Restaurant fields = ['name', 'desc', 'min_order', 'facebook', 'twitter', 'instagram', 'website', 'logo', 'photoshoot_requested', 'opening_hours'] widgets = { 'name': widgets.TextInput(attrs={'class': 'lb-name', 'placeholder': _('Name of the restaurant')}), 'desc': widgets.Textarea(attrs={'class': 'lb-name', 'placeholder': _('DESCRIPTION: Describe your restaurant!')}), 'min_order': widgets.TextInput(attrs={'class': 'd2','placeholder': 'XX'}), 'facebook': widgets.TextInput(attrs={'class': 'lb-site', 'placeholder': _('Enter your facebook address')}), 'twitter': widgets.TextInput(attrs={'class': 'lb-site', 'placeholder': _('Your twitter')}), 'instagram': widgets.TextInput(attrs={'class': 'lb-site', 'placeholder': _('Your instagram')}), 'website': widgets.TextInput(attrs={'class': 'lb-site', 'placeholder': _('Your website')}), 'logo': widgets.FileInput(attrs={ 'class': 'image_upload_btn' }), 'opening_hours': widgets.Textarea(attrs={'class': 'lb-name', 'placeholder': _('Opening hours (example):\nMon: Closed\nTue - Thu: 10:00 - 00:00\nFri - Sun: 10:00 - 02:00')}) }
class User(forms.Form): user = forms.CharField(max_length=32, label='用户名', error_messages={'required': '用户名不能为空'}, widget=widgets.TextInput( attrs={'class': 'form-control'}, )) pwd = forms.CharField(max_length=32, label='密码', error_messages={'required': '密码不能为空'}, widget=widgets.PasswordInput( attrs={'class': 'form-control'}, )) e_pwd = forms.CharField(max_length=32, label='确认密码', error_messages={'required': '密码确认不能为空'}, widget=widgets.PasswordInput( attrs={'class': 'form-control'}, )) email = forms.EmailField(max_length=32, label='邮箱', error_messages={'required': '邮箱不能为空'}, widget=widgets.EmailInput( attrs={'class': 'form-control'}, )) def clean_user(self): # 用户名钩子 user = self.cleaned_data.get('user') ret = UserInfo.objects.filter(username=user).first() if not ret: # 没被注册过 return user else: raise ValidationError('该用户名已被注册!') def clean(self): # 全局钩子,判断密码是否一致 pwd = self.cleaned_data.get('pwd') e_pwd = self.cleaned_data.get('e_pwd') if pwd and e_pwd: # 如果有一个没输入,就不必判断密码是否一致了 if pwd == e_pwd: return self.cleaned_data else: raise ValidationError('两次密码不一致!')
class UserForm(forms.Form): user = forms.CharField(max_length=32, label="用户名", error_messages={"required":"该字段不能为空"}, widget=widgets.TextInput(attrs={"class": "form-control"}, ) ) pwd = forms.CharField(max_length=32, label="密码", error_messages={"required": "该字段不能为空"}, widget=widgets.PasswordInput(attrs={"class": "form-control"}, ) ) re_pwd = forms.CharField(max_length=32, label="确认密码", error_messages={"required": "该字段不能为空"}, widget=widgets.PasswordInput(attrs={"class": "form-control"}, ) ) email = forms.EmailField(max_length=32, label="邮箱", error_messages={"required": "该字段不能为空"}, widget=widgets.EmailInput(attrs={"class": "form-control"}, ) ) def clean_user(self): user=self.cleaned_data.get("user") user_object=models.UserInfo.objects.filter(username=user).first() if not user_object: return user else: raise ValidationError("该用户已注册") def clean(self): pwd=self.cleaned_data.get("pwd") re_pwd=self.cleaned_data.get("re_pwd") if pwd==re_pwd: return self.cleaned_data else: raise ValidationError("两次密码不一致")
def resultinput_view(self, request, course_record_id): """成绩录入视图""" if request.method == 'GET': study_record_obj_list = models.StudyRecord.objects.filter(course_record_id=course_record_id) data_list = [] for stu_record_obj in study_record_obj_list: tmp_dict = {"stu_record_obj": stu_record_obj, "form": None} TempForm = type('TempForm', (Form,), { "score_{}".format(stu_record_obj.pk): fields.ChoiceField( choices=stu_record_obj.score_choices, widget=widgets.Select(attrs={"class": 'form-control'})), "homework_note_{}".format(stu_record_obj.pk): fields.CharField( widget=widgets.TextInput(attrs={"class": 'form-control'})) }) tmp_dict["form"] = TempForm(initial={ "score_{}".format(stu_record_obj.pk): stu_record_obj.score, "homework_note_{}".format(stu_record_obj.pk): stu_record_obj.homework_note, }) data_list.append(tmp_dict) return render(request, 'resultinput_view.html', {"data_list": data_list}) else: tmp_dict = {} for field_and_pk, value in request.POST.items(): if field_and_pk == 'csrfmiddlewaretoken': continue field, pk = field_and_pk.rsplit('_', 1) if pk not in tmp_dict: tmp_dict[pk] = {field: value} else: tmp_dict[pk][field] = value for pk, dict_item in tmp_dict.items(): models.StudyRecord.objects.filter(pk=pk).update(**dict_item) return redirect(reverse('%s_%s_changelist' % (self.app_label, self.model_name)))
class TestForm(forms.Form): user = fields.CharField( required=True, # 是否必填 max_length=12, # 最大长度 min_length=3, # 最小长度 error_messages={}, # 错误提示 widget=widgets.TextInput(attrs={'class': 'c1'}), # 定制HTML插件 label='用户名', initial='请输入用户', help_text='asdfasdf', show_hidden_initial=False, # validators=[] disabled=True, label_suffix='->') age = fields.IntegerField(label='年龄', max_value=12, min_value=5, error_messages={'max_value': '太大了'}) email = fields.EmailField(label='邮箱') img = fields.FileField() city = fields.TypedChoiceField(coerce=lambda x: int(x), choices=[ ( 1, '上海', ), (2, '北京'), (3, '沙河'), ], initial=2) bobby = fields.MultipleChoiceField(choices=[(1, '刚娘'), (2, '铁娘'), (3, '钢弹')], initial=[1, 2]) xoo = fields.FilePathField(path='app01')
class UserForm(forms.Form): user = forms.CharField( max_length=32, error_messages={'required': '该字段不能为空'}, widget=widgets.TextInput(attrs={'class': 'form-control'}), label='用户名') pwd = forms.CharField( max_length=32, widget=widgets.PasswordInput(attrs={'class': 'form-control'}), label='密码') re_pwd = forms.CharField( max_length=32, widget=widgets.PasswordInput(attrs={'class': 'form-control'}), label='确认密码') email = forms.EmailField( max_length=32, widget=widgets.EmailInput(attrs={'class': 'form-control'}), label='邮箱') def clean_user(self): val = self.cleaned_data.get("user") user = UserInfo.objects.filter(username=val).first() if not user: return val else: raise ValidationError("该用户已注册!") def clean(self): pwd = self.cleaned_data.get('pwd') re_pwd = self.cleaned_data.get('re_pwd') if pwd and re_pwd: if pwd == re_pwd: return self.cleaned_data else: raise ValidationError('两次密码不一致') else: return self.cleaned_data
class HeadingPlugin(CascadePluginBase): name = _("Heading") parent_classes = None allow_children = False TAG_TYPES = tuple( ('h{}'.format(k), _("Heading {}").format(k)) for k in range(1, 7)) glossary_field_order = ['tag_type', 'content', 'element_id'] tag_type = GlossaryField(widgets.Select(choices=TAG_TYPES)) content = GlossaryField(widgets.TextInput( attrs={ 'style': 'width: 100%; padding-right: 0; font-weight: bold; font-size: 125%;' }), label=_("Heading content")) render_template = 'cascade/generic/heading.html' class Media: css = {'all': ('cascade/css/admin/partialfields.css', )} @classmethod def get_identifier(cls, instance): identifier = super(HeadingPlugin, cls).get_identifier(instance) tag_type = instance.glossary.get('tag_type') content = mark_safe(instance.glossary.get('content', '')) if tag_type: return format_html('<code>{0}</code>: {1} {2}', tag_type, content, identifier) return content def render(self, context, instance, placeholder): context = self.super(HeadingPlugin, self).render(context, instance, placeholder) context.update( {'content': mark_safe(instance.glossary.get('content', ''))}) return context
class User(forms.Form): user = forms.CharField( min_length=6, label='用户名', widget=widgets.TextInput(attrs={'class': 'form-control'}), error_messages={'required': '输入字段不能为空'}) pwd = forms.CharField( min_length=6, label='密码', widget=widgets.PasswordInput(attrs={'class': 'form-control'}), error_messages={'required': '输入字段不能为空'}) r_pwd = forms.CharField( min_length=6, label='确认密码', widget=widgets.PasswordInput(attrs={'class': 'form-control'}), error_messages={'required': '输入字段不能为空'}) email = forms.EmailField( widget=widgets.EmailInput(attrs={'class': 'form-control'}), error_messages={'required': '输入字段不能为空'}) def clean_user(self): name = self.cleaned_data.get('user') user = UserInfo.objects.filter(username=name).first() if user: raise ValidationError('该用户已存在!') else: return name def clean(self): pwd = self.cleaned_data.get('pwd') r_pwd = self.cleaned_data.get('r_pwd') if pwd and r_pwd: if pwd == r_pwd: return self.cleaned_data else: raise ValidationError('两次密码不一致!') else: return self.cleaned_data
class AccountForm(forms.Form): """ A form used to add and edit a user account. When editing an account, the email field is readonly. """ email = forms.EmailField(widget=widgets.TextInput(attrs={'size': 40})) userlevel = forms.CharField( label="Role", widget=widgets.Select(choices=STARS_USERLEVEL_CHOICES)) def __init__(self, *args, **kwargs): super(AccountForm, self).__init__(*args, **kwargs) if 'email' in self.initial: self.fields['email'].widget.attrs['readonly'] = True def clean_email(self): """ Guards against nasty POSTs that try to change the email. """ try: return self.initial['email'] except KeyError: return self.cleaned_data.get('email', None)
def __init__(self, *args, **kwargs): super(RegisterForm, self).__init__(*args, **kwargs) self.fields['username'].widget = widgets.TextInput( attrs={ 'placeholder': "用户名", "class": "form-control" }) self.fields['email'].widget = widgets.EmailInput( attrs={ 'placeholder': "邮箱", "class": "form-control" }) self.fields['password1'].widget = widgets.PasswordInput( attrs={ 'placeholder': "密码", "class": "form-control" }) self.fields['password2'].widget = widgets.PasswordInput( attrs={ 'placeholder': "确认密码", "class": "form-control" })
def __init__(self, *args, **kwargs): super(RegisterForm, self).__init__(*args, **kwargs) self.fields['username'].widget = widgets.TextInput( attrs={ 'placeholder': '请输入用户名', 'class': 'form-control my-xl-3' }) self.fields['email'].widget = widgets.EmailInput( attrs={ 'placeholder': '电子邮件', 'class': 'form-control my-xl-3' }) self.fields['password1'].widget = widgets.PasswordInput( attrs={ 'placeholder': '输入密码', 'class': 'form-control my-xl-3' }) self.fields['password2'].widget = widgets.PasswordInput( attrs={ 'placeholder': '再次输入密码', 'class': 'form-control my-xl-3' })
class LoginForm(forms.Form): email = forms.EmailField( required=True, error_messages={'required': '邮箱不能为空'}, widget=widgets.EmailInput(attrs={'class': "form-control"})) pwd = forms.CharField( required=True, min_length=5, error_messages={ 'required': '密码不能为空!', 'min_length': '密码小于5位!' }, widget=widgets.PasswordInput(attrs={'class': "form-control"})) code = forms.CharField(required=True, min_length=4, error_messages={ 'required': '验证码不能为空!', 'min_length': '验证码不能小于4位!' }, widget=widgets.TextInput(attrs={ 'class': "form-control", 'placeholder': '请输入验证码' }))
class ForgetPwdForms(forms.Form): email = forms.EmailField(max_length=30,error_messages={'required': '验证码不能为空'}) valid_code = forms.CharField(min_length=6, error_messages={'required': '验证码不能为空', 'min_length': '至少为6位'}, max_length=6, widget=widgets.TextInput(attrs={"placeholder": "验证码"})) def __init__(self, request, *args, **kwargs): super(ForgetPwdForms, self).__init__(*args, **kwargs) self.request = request def clean_email(self): email = self.cleaned_data['email'] user = UserInfo.objects.filter(email=email) if user: return email raise ValidationError('该邮箱没有注册') def clean_valid_code(self): '''检测验证码是否匹配''' if self.cleaned_data["valid_code"].upper() == self.request.session["valid_code"].upper(): return self.cleaned_data["valid_code"] else: print('验证码错误') raise ValidationError("验证码错误!")
class SearchForm(forms.Form): q = forms.CharField( widget=widgets.TextInput( attrs={ 'placeholder': 'Search...', 'class': 'form-control' } ), validators=[MinLengthValidator(1)]) type = forms.ChoiceField( initial='track', widget=widgets.HiddenInput(), choices=( ('track', 'Track'), ('album', 'Album'), ('artist', 'Artist'), ('playlist', 'Playlist'), )) def query_api(self): clean_data = self.cleaned_data url = SPOTIFY_API['search'] + urlencode(clean_data) return requests.get(url)
class newsform(forms.Form): catelist = cate.objects.filter(pcate__isnull=False) #二级和三级分类 fcatelist = cate.objects.filter(pcate__isnull=True) #一级分类 scatelist = cate.objects.filter(pcate__in=fcatelist) #二级分类 tcatelist = cate.objects.filter(pcate__in=scatelist) #三级分类 stcatelist = scatelist | tcatelist #二级和三级分类合并 cate = forms.ModelChoiceField(queryset=stcatelist, label="类别", initial=catelist.first().name) title = forms.CharField(max_length=100, label="标题", widget=widgets.TextInput(attrs={'size': '50%'})) img = forms.ImageField(label="图片封面", allow_empty_file=True, required=False) content = forms.CharField(label="内容", widget=UEditorWidget({ "width": "98%", "height": 400, "imagePath": 'pic/', "filePath": 'upfiles/' }))
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['username'].widget = widgets.TextInput( attrs={ 'placeholder': _("username"), "class": "form-control" }) self.fields['email'].widget = widgets.EmailInput( attrs={ 'placeholder': _("Email"), "class": "form-control" }) self.fields['password1'].widget = widgets.PasswordInput( attrs={ 'placeholder': _("Password"), "class": "form-control" }) self.fields['password2'].widget = widgets.PasswordInput( attrs={ 'placeholder': _("Password confirmation"), "class": "form-control" })
class Meta: model = models.Questionnaire fields = '__all__' error_messages = { "title": { "required": '标题不能为空' }, "classroom": { "required": '调查班级不能为空' }, } widgets = { "title": wd.TextInput(attrs={ "class": 'form-control', "aria-describedby": 'help-title' }), "classroom": wd.Select(attrs={ "class": 'form-control', "aria-describedby": 'help-classroom' }), }
class Login(Form): username = fields.CharField( required=True, widget=widgets.TextInput(attrs={"placeholder": "用户名"}), error_messages={"required": "用户名不能为空!"} ) password = fields.CharField( required=True, min_length=3, max_length=6, error_messages={"required": "密码不能为空!", "min_length": "最小为3位!", "max_length": "最大为6位!"}, validators=[RegexValidator("\d+", "只能是数字!!")], widget=widgets.PasswordInput(attrs={"placeholder": "密码"}) ) def clean_username(self): user = self.cleaned_data.get("username") is_exit = models.User.objects.filter(username=user) if not is_exit: raise ValidationError("用户名不存在!!") return user
def __init__(self, *args, **kwargs): super(RegisterForm, self).__init__(*args, **kwargs) self.fields['username'].widget = widgets.TextInput( attrs={ 'placeholder': 'username', 'class': 'form-control' }) self.fields['email'].widget = widgets.EmailInput( attrs={ 'placeholder': 'email', 'class': 'form-control' }) self.fields['password1'].widget = widgets.PasswordInput( attrs={ 'placeholder': 'password', 'class': 'form-control' }) self.fields['password2'].widget = widgets.PasswordInput( attrs={ 'placeholder': 'repeat password', 'class': 'form-control' })
class RegisterForm(forms.Form): gender = ( ('male', '男'), ('female', '女'), ) name = fields.CharField( label='用户名:', max_length=128, widget=widgets.TextInput(attrs={'class': 'form-control'})) password1 = fields.CharField( label='密码', max_length=256, widget=widgets.PasswordInput(attrs={'class': 'form-control'})) password2 = fields.CharField( label='确认密码', max_length=256, widget=widgets.PasswordInput(attrs={'class': 'form-control'})) email = fields.EmailField( label='邮箱地址', widget=widgets.EmailInput(attrs={'class': 'form-control'})) sex = fields.ChoiceField(label='性别', choices=gender) captcha = CaptchaField(label='验证码')
class ContactForm(forms.Form): CITY = [['TP', 'Taipei'], ['TY', 'Taoyuang']] user_name = forms.CharField(label='用户名', max_length=50, initial='niu', widget=widgets.TextInput(attrs={ 'class': 'form-control', 'placeholder': '用户名' })) user_city = forms.ChoiceField( label='城市', choices=CITY, widget=widgets.Select(attrs={ 'class': 'form-control', })) user_message = forms.CharField( label='你的意见', widget=forms.Textarea(attrs={ 'class': 'form-control', })) def clean_user_name(self): user = self.cleaned_data.get('username') raise ValidationError('用户名不存在')
class Meta: model = models.UserInfo fields = ['username', 'password'] error_messages = { "username": { 'required': '用户名不能为空' }, "password": { 'required': '密码不能为空' }, } widgets = { "username": wd.TextInput(attrs={ "placeholder": 'Username', "class": 'form-control' }), "password": wd.PasswordInput(attrs={ "placeholder": 'Password', "class": 'form-control' }), }
class Meta: # Models中的哪个model model = UserInfo # 使用该model中的哪些属性(字段) fields = ['username', 'password'] # 参数 widgets = { 'username': wid.TextInput( attrs={ 'class': "form-control", 'placeholder': "用户名", 'autocomplete': "off" }), 'password': wid.PasswordInput( attrs={ 'class': "form-control", 'placeholder': "密码", 'autocomplete': "off" }) }
class ProjectForms(forms.Form): """ 项目表单 """ name = forms.CharField( label="项目名称", max_length=100, widget=widgets.TextInput(attrs={"class": "form-control"})) describe = forms.CharField( label="项目描述", max_length=200, widget=widgets.Textarea(attrs={"class": "form-control"})) status = forms.BooleanField(label="状态", required=False, widget=widgets.CheckboxInput()) def clean_name(self): """ 验证项目名称 """ name = self.cleaned_data['name'] if name is None: raise forms.ValidationError('请输入项目名称') project_name = Project.objects.filter(name=name) if project_name: raise forms.ValidationError('项目名称已存在,请重新输入') return name
class Meta: model = Buyer exclude = ['id', 'credit_card'] widgets = { 'buyer_name': widgets.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Type in your full name.' }), 'address': widgets.TextInput( attrs={ 'class': 'form-control', 'placeholder': 'Type in your current streetname.' }), 'city': widgets.TextInput( attrs={ 'class': 'form-control', 'placeholder': 'Type in your current city.' }), 'country': widgets.Select( attrs={ 'class': 'form-control', 'placeholder': 'Type in your current country of living.' }), 'postal_code': widgets.TextInput( attrs={ 'class': 'form-control', 'placeholder': 'Type in your current postal code.' }), 'ssc': widgets.TextInput( attrs={ 'class': 'form-control', 'placeholder': 'Type in your social security number.' }), 'email': widgets.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Type in your email.' }), }
def __init__(self, *args, **kwargs): super(UserRegistrationForm, self).__init__(*args, **kwargs) self.fields['username'].widget = widgets.TextInput( attrs={ 'placeholder': "username", "class": "form-control" }) self.fields['email'].widget = widgets.EmailInput( attrs={ 'placeholder': "email", "class": "form-control" }) self.fields['password1'].widget = widgets.PasswordInput( attrs={ 'placeholder': "password", "class": "form-control" }) self.fields['password2'].widget = widgets.PasswordInput( attrs={ 'placeholder': "repeat password", "class": "form-control" })
class loginForm(forms.Form): username = fields.CharField( required=True, widget=widgets.TextInput(attrs={ 'class': "input", 'placeholder': '请输入用户名' }), min_length=4, max_length=12, error_messages={ 'required': '用户名不能为空', }) pwd = fields.CharField(widget=widgets.PasswordInput(attrs={ 'class': "input", 'placeholder': '请输入密码' }), required=True, min_length=6, max_length=12, error_messages={ 'required': '密码不能为空!', })
class LoginForm(forms.Form): username = fields.CharField( required=True, widget=widgets.TextInput(attrs={'class': "form-control",'placeholder': '用户名'}), error_messages={'required': '用户名不能为空',} ) pwd = fields.CharField( widget=widgets.PasswordInput(attrs={'class': "form-control",'placeholder': '密码'}), required=True, strip=True, error_messages={'required': '密码不能为空!',} ) def clean(self): username = self.cleaned_data.get('username') pwd = self.cleaned_data.get('pwd') user = User.objects.filter(username=username).first() if username and pwd: if not user : self.add_error('username','用户名不存在!') elif pwd != user.pwd: self.add_error('pwd','用户名与密码不匹配!')
class UserForm(forms.Form): user = forms.CharField(max_length=32, error_messages={"required": "该字段不能为空"}, label="用户名", widget=widgets.TextInput( attrs={"class": "form-control"}, )) pwd = forms.CharField(max_length=32, label="密码", widget=widgets.PasswordInput( attrs={"class": "form-control"}, )) re_pwd = forms.CharField(max_length=32, label="确认密码", widget=widgets.PasswordInput( attrs={"class": "form-control"}, )) email = forms.EmailField(max_length=32, label="邮箱", widget=widgets.EmailInput( attrs={"class": "form-control"}, )) def clean_user(self): val = self.cleaned_data.get('user') user = UserInfo.objects.filter(username=val).first() if not user: return val else: raise ValidationError('该用户已存在') def clean(self): pwd = self.cleaned_data.get('pwd') re_pwd = self.cleaned_data.get('re_pwd') if pwd and re_pwd: if pwd == re_pwd: return self.cleaned_data else: raise ValidationError('两次密码不一致') else: return self.clean