class Meta: model = Employee fields = [ 'company', 'image', 'facebook', 'twitter', 'github', 'linkedin' ] widgets = { 'company': widgets.Select(attrs={'class': 'form-control'}), 'image': widgets.FileInput(attrs={'class': 'form-control'}), 'facebook': widgets.URLInput(attrs={'class': 'form-control'}), 'twitter': widgets.URLInput(attrs={'class': 'form-control'}), 'github': widgets.URLInput(attrs={'class': 'form-control'}), 'linkedin': widgets.URLInput(attrs={'class': 'form-control'}), }
class EditForm(forms.Form): username = forms.CharField(max_length=32, label="用户名", error_messages={"required": "用户名不能为空"}, widget=widgets.TextInput(attrs={"class": "form-control"}) ) chinaName = forms.CharField(max_length=16, label="姓名", error_messages={"required": "姓名不能为空"}, widget=widgets.TextInput(attrs={"class": "form-control"}) ) addr = forms.CharField(max_length=32, label="地址", error_messages={"required": "地址不能为空"}, widget=widgets.TextInput(attrs={"class": "form-control"})) url = forms.URLField(max_length=64, label="博客地址", widget=widgets.URLInput(attrs={"class": "form-control"}), error_messages={"required": "URL不能为空", "invalid": "URL格式不正确"}) email = forms.EmailField(label='电子邮箱', error_messages={"required": "电子邮箱不能为空", "invalid": "电子邮箱格式不正确"}, widget=widgets.EmailInput(attrs={"class": "form-control"})) def clean_username(self): from Luffy.models import UserInfo user = self.cleaned_data.get("username") flag = UserInfo.objects.filter(username=user).first() if not flag: return user else: raise ValidationError("该用户已被注册")
class BootstrapYoutubeForm(ModelForm): url = URLField( label=_("YouTube URL"), widget=widgets.URLInput(attrs={'size': 50}), ) def __init__(self, *args, **kwargs): instance = kwargs.get('instance') if instance: videoid = instance.glossary.get('videoid') if videoid: parts = ParseResult('https', 'youtu.be', videoid, '', '', '') initial = {'url': urlunparse(parts)} kwargs.update(initial=initial) super(BootstrapYoutubeForm, self).__init__(*args, **kwargs) def clean(self): cleaned_data = super(BootstrapYoutubeForm, self).clean() url = cleaned_data.pop('url', None) if url: parts = urlparse(url) match = re.search(r'([^/]+)$', parts.path) if match: cleaned_data['glossary']['videoid'] = match.group(0) return cleaned_data raise ValidationError(_("Please enter a valid YouTube URL"))
class Meta: model = BannerImages exclude = ['id'] widgets = { 'imageURL': widgets.URLInput(attrs={'class': 'form-control'}), 'product': widgets.Select(attrs={'class': 'form-control'}) }
class Meta: model = ProductImage exclude = ['id'] widgets = { 'image': widgets.URLInput(attrs={'class': 'form-control'}), 'product': widgets.HiddenInput(attrs={'class': 'form-control'}), }
class AddGitForm(Form): title = fields.CharField( label="标题", widget=widgets.TextInput(attrs={ "id": "person_title", "class": "form-control" }), error_messages={"required": "标题不能为空"}, ) url = fields.URLField( label="根URL", widget=widgets.URLInput(attrs={ "id": "gitlab_url", "class": "form-control" }), error_messages={"required": "URL不能为空"}, ) person_token = fields.CharField( label="个人访问令牌", widget=widgets.TextInput(attrs={ "id": "person_token", "class": "form-control" }), error_messages={"required": "个人访问令牌不能为空"}, ) myuser_id = fields.IntegerField(widget=widgets.HiddenInput, )
class Meta: model = Article fields = ('url', ) widgets = { 'url': widgets.URLInput(attrs={ 'placeholder': 'Enter a blog URL', }) }
class EmbedForm(PlusPluginFormBase): url = forms.URLField( label=_("Media URL"), widget=widgets.URLInput(attrs={'size': 50}), help_text=_( 'Video Url to an external service w/o query params such as YouTube, Vimeo or others, ' 'e.g.: ' 'https://www.youtube.com/embed/vZw35VUBdzo'), ) ASPECT_RATIO_CHOICES = [ ('embed-responsive-21by9', _("Responsive 21:9")), ('embed-responsive-16by9', _("Responsive 16:9")), ('embed-responsive-4by3', _("Responsive 4:3")), ('embed-responsive-1by1', _("Responsive 1:1")), ] aspect_ratio = forms.ChoiceField( label=_("Aspect Ratio"), choices=ASPECT_RATIO_CHOICES, widget=widgets.RadioSelect, required=False, initial=ASPECT_RATIO_CHOICES[1][0], ) allow_fullscreen = forms.BooleanField( label=_("Allow Fullscreen"), required=False, initial=True, ) autoplay = forms.BooleanField( label=_("Autoplay"), required=False, ) controls = forms.BooleanField( label=_("Display Controls"), required=False, ) loop = forms.BooleanField( label=_("Enable Looping"), required=False, help_text=_('Inifinte loop playing.'), ) rel = forms.BooleanField( label=_("Show related"), required=False, help_text=_('Show related media content'), ) STYLE_CHOICES = 'EMBED_STYLES' extra_style, extra_classes, label, extra_css = get_style_form_fields( STYLE_CHOICES)
class Meta: model = User exclude = [ 'id', 'last_login', 'is_superuser', 'is_staff', 'is_active', 'date_joined', 'username', 'password', 'email', 'first_name', 'last_name', 'enabled', 'address', 'user_permissions', 'groups' ] widgets = { 'image': widgets.URLInput(attrs={'class': 'form-control'}, ), }
class TestForm(forms.Form): nom = CharField(widget=widgets.TextInput(attrs={'placeholder': 'Nom'})) prenom = CharField(widget=widgets.TextInput( attrs={'placeholder': 'Prenom'})) email = EmailField(widget=widgets.TextInput( attrs={'placeholder': 'Email'})) password = CharField(widget=widgets.PasswordInput( attrs={'placeholder': 'Password'})) facebook = URLField(widget=widgets.URLInput( attrs={'placeholder': 'Facebook link'})) pic = ImageField(max_length=20, allow_empty_file=True) picture = FileField() another = ChoiceField(choices=STARS)
class Meta: model = User exclude = ['id', 'last_login', 'is_superuser', 'is_active', 'date_joined', 'groups', 'user_permissions', 'address'] widgets = { 'username': widgets.TextInput(attrs={'class': 'form-control'}), 'password': widgets.PasswordInput(attrs={'class': 'form-control'}), 'email': widgets.EmailInput(attrs={'class': 'form-control'}), 'first_name': widgets.TextInput(attrs={'class': 'form-control'}), 'last_name': widgets.TextInput(attrs={'class': 'form-control'}), 'image': widgets.URLInput(attrs={'class': 'form-control'}), 'enabled': widgets.NullBooleanSelect(attrs={'class': 'form-control'}), # Add option to create staff or superuser? }
class ProfileForm(forms.Form): username = forms.CharField(max_length=16, widget=widgets.Input( attrs={ 'type': "username", 'class': "form-control", 'id': "exampleInputUsername", 'readonly': True })) email = forms.EmailField( max_length=32, widget=widgets.EmailInput(attrs={ 'type': "email", 'class': "form-control", 'id': "exampleInputEmail" })) bio = forms.CharField( max_length=256, widget=widgets.Textarea(attrs={'class': "form-control"}), required=False) url = forms.URLField( max_length=256, widget=widgets.URLInput(attrs={'class': "form-control"}), required=False) location = forms.CharField( max_length=32, widget=widgets.Input(attrs={'class': "form-control"}), required=False) avatar = forms.ImageField(allow_empty_file=True) def set_initial_fields(self, user=None): if user: if user.username: self.fields['username'].initial = user.username if user.email: self.fields['email'].initial = user.email if user.userinfo: if user.userinfo.bio: self.fields['bio'].initial = user.userinfo.bio if user.userinfo.url: self.fields['url'].initial = user.userinfo.url if user.userinfo.location: self.fields['location'].initial = user.userinfo.location if user.userinfo.avatar: self.fields['avatar'].initial = user.userinfo.avatar
class InfoPersonForm(forms.ModelForm): SEX_CHOICES = ( (1, "男"), (2, "女"), ) AGE_CHOICES = ( (1, "30岁以下"), (2, "31-40岁"), (3, "41-50岁"), (4, "51岁以上"), ) EDUCATION_CHOICES = ( (1, "初中"), (2, "高中(中专)"), (3, "大专"), (4, "本科"), (5, "本科以上"), ) company_name = forms.CharField( error_messages={'required': '企业名称不能为空'}, widget=widgets.TextInput(attrs={ 'placeholder': "请输入企业全称", 'class': "form-control" })) social_credit_code = forms.CharField( max_length=18, error_messages={'required': '社会统一信用代码不能为空'}, widget=widgets.TextInput(attrs={'class': "form-control"})) established_time = forms.DateField(error_messages={'required': '成立时间不能为空'}, widget=widgets.DateInput(attrs={ 'class': "form-control", 'type': "date" })) responsible_person = forms.CharField( error_messages={'required': '负责人不能为空'}, widget=widgets.TextInput(attrs={'class': "form-control"})) job_title = forms.CharField( error_messages={'required': '职务或职称不能为空'}, widget=widgets.TextInput(attrs={'class': "form-control"})) sex = forms.ChoiceField(error_messages={'required': '性别不能为空'}, choices=SEX_CHOICES, widget=widgets.RadioSelect()) age = forms.ChoiceField(error_messages={'required': '年龄不能为空'}, choices=AGE_CHOICES, widget=widgets.RadioSelect()) degree_of_education = forms.ChoiceField( error_messages={'required': '文化程度不能为空'}, choices=EDUCATION_CHOICES, widget=widgets.RadioSelect()) phone = forms.CharField( max_length=11, error_messages={'required': '手机号不能为空'}, widget=widgets.TextInput(attrs={ 'placeholder': "为方便联系,请您填写手机号。", 'class': "form-control" })) political_status = forms.CharField( error_messages={'required': '政治面貌不能为空'}, widget=widgets.TextInput(attrs={'class': "form-control"})) company_registered_address = forms.CharField( error_messages={'required': '企业注册地址不能为空'}, widget=widgets.TextInput(attrs={'class': "form-control"})) website_url = forms.URLField( error_messages={'required': '企业网址不能为空'}, widget=widgets.URLInput(attrs={ 'placeholder': "网址、微信公众号全称、博客网址", 'class': "form-control" })) email_adress = forms.EmailField( error_messages={'required': 'email地址不能为空'}, widget=widgets.EmailInput(attrs={'class': "form-control"})) company_profiles = forms.CharField(widget=widgets.Textarea(attrs={ 'class': "form-control", 'rows': "3" })) # 使用ModelForm时的内部类 class Meta: model = InformationOfPerson exclude = ['questionnaire']
class URLForm(forms.ModelForm): url = forms.URLField(widget=widgets.URLInput( attrs={'class': 'input-xxlarge'}))
class YoutubeFormMixin(EntangledModelFormMixin): ASPECT_RATIO_CHOICES = [ ('embed-responsive-21by9', _("Responsive 21:9")), ('embed-responsive-16by9', _("Responsive 16:9")), ('embed-responsive-4by3', _("Responsive 4:3")), ('embed-responsive-1by1', _("Responsive 1:1")), ] videoid = EntangledField() url = URLField( label=_("YouTube URL"), widget=widgets.URLInput(attrs={'size': 50}), ) aspect_ratio = ChoiceField( label=_("Aspect Ratio"), choices=ASPECT_RATIO_CHOICES, widget=widgets.RadioSelect, initial=ASPECT_RATIO_CHOICES[1][0], ) allow_fullscreen = BooleanField( label=_("Allow Fullscreen"), required=False, initial=True, ) autoplay = BooleanField( label=_("Autoplay"), required=False, ) controls = BooleanField( label=_("Display Controls"), required=False, ) loop = BooleanField( label=_("Enable Looping"), required=False, ) rel = BooleanField( label=_("Show related"), required=False, ) class Meta: untangled_fields = ['url'] entangled_fields = { 'glossary': [ 'videoid', 'aspect_ratio', 'allow_fullscreen', 'autoplay', 'controls', 'loop', 'rel' ] } def __init__(self, *args, **kwargs): instance = kwargs.get('instance') if instance: videoid = instance.glossary.get('videoid') if videoid: parts = ParseResult('https', 'youtu.be', videoid, '', '', '') initial = {'url': urlunparse(parts)} kwargs.update(initial=initial) super().__init__(*args, **kwargs) def clean(self): cleaned_data = super().clean() url = cleaned_data.get('url') if url: parts = urlparse(url) match = re.search(r'^v=([^&]+)', parts.query) if match: cleaned_data['videoid'] = match.group(1) return cleaned_data match = re.search(r'([^/]+)$', parts.path) if match: cleaned_data['videoid'] = match.group(1) return cleaned_data raise ValidationError(_("Please enter a valid YouTube URL"))
class RegisterForm(Form): """ 注册form """ username = fields.CharField( widget=widgets.TextInput(attrs={ 'placeholder': u'用户名', "class": "form-control" }), max_length=18, min_length=6, required=True, error_messages={ 'required': "用户名不能为空", 'max_length': "用户名长度不能超过18位", 'min_length': "用户名长度不能少于6位", }) email = fields.EmailField(widget=widgets.EmailInput(attrs={ 'placeholder': u'邮箱', "class": "form-control" }), required=True, error_messages={ 'required': "邮箱不能为空", }) url = fields.URLField(widget=widgets.URLInput(attrs={ 'placeholder': u'个人博客地址', "class": "form-control" }), initial="http://", required=True, error_messages={'required': "个人博客地址不能为空"}) password = fields.CharField( widget=widgets.TextInput(attrs={ 'placeholder': u'密码', "class": "form-control" }), max_length=16, min_length=8, required=True, error_messages={ 'required': "密码不能为空", 'max_length': "密码长度不能超过18位", 'min_length': "密码长度不能少于8位", }) check_password = fields.CharField( widget=widgets.TextInput(attrs={ 'placeholder': u'请再次输入密码', "class": "form-control" }), max_length=16, min_length=8, required=True, error_messages={ 'required': "密码不能为空", 'max_length': "密码长度不能超过18位", 'min_length': "密码长度不能少于8位", }) def clean_username(self): val = self.cleaned_data.get("username") ret = User.objects.filter(username=val) if not ret: return val else: raise ValidationError("该用户已注册") def clean_email(self): val = self.cleaned_data.get("email") ret = User.objects.filter(email=val) if not ret: return val else: raise ValidationError("该邮箱已被注册") def clean_check_password(self): pwd = self.cleaned_data.get("password") ck_pwd = self.cleaned_data.get("check_password") if pwd and ck_pwd: if pwd == ck_pwd: return ck_pwd else: raise ValidationError('两次密码不一致') else: raise ValidationError("请填写密码")
PizzeriaLocation, form=LocationForm, fields=( 'street_address1', 'street_address2', 'city', 'state', 'zip_code', 'phone', 'location_website', 'dine_in', 'carry_out', 'delivery', ), extra=1, max_num=10, absolute_max=10, widgets={ 'street_address1': widgets.TextInput(attrs={'class': formset_classes, 'placeholder': ' ', 'required': 'true'}), 'street_address2': widgets.TextInput(attrs={'class': formset_classes, 'placeholder': ' '}), 'city': widgets.TextInput(attrs={'class': formset_classes, 'placeholder': ' ', 'required': 'true'}), 'state': widgets.TextInput(attrs={'class': formset_classes, 'placeholder': ' ', 'required': 'true'}), 'zip_code': widgets.TextInput(attrs={'class': formset_classes, 'placeholder': ' ', 'required': 'true'}), 'phone': widgets.TextInput(attrs={'class': formset_classes, 'placeholder': ' '}), 'location_website': widgets.URLInput(attrs={'class': formset_classes, 'placeholder': ' '}), 'dine_in': widgets.CheckboxInput(attrs={'class': checkbox_classes}), 'carry_out': widgets.CheckboxInput(attrs={'class': checkbox_classes}), 'delivery': widgets.CheckboxInput(attrs={'class': checkbox_classes}), } ) class MenuItemForm(forms.ModelForm): class Meta: model = MenuItem fields = ( 'title', 'description', 'price', 'photo', ) widgets = {