def __init__(self, request, *args, **kwargs): field_name = kwargs.pop('field_name') self.request = request super(DateRangeExForm, self).__init__(*args, **kwargs) self.fields['%s%s__gte' % (FILTER_PREFIX, field_name)] = forms.DateField( label='', widget=DatePickerWidget(attrs={ 'class': 'date', 'placeholder': 'Desde el día' }, format="%m/%d/%Y"), input_formats=('%Y/%m/%d', '%d/%m/%Y', '%m/%d/%Y'), localize=True, required=False) self.fields['%s%s__lte' % (FILTER_PREFIX, field_name)] = forms.DateField( label='', widget=DatePickerWidget(attrs={ 'class': 'date', 'placeholder': 'Hasta el día', }, format="%m/%d/%Y"), input_formats=('%Y/%m/%d', '%d/%m/%Y', '%m/%d/%Y'), localize=True, required=False, )
class ProfileUpdateForm(ModelForm): ''' ModelForm maps AboutMe Model to a Form ''' class Meta: model = AboutMe widgets = { 'first_name': forms.TextInput(attrs={'class': 'form_widget'}), 'last_name': forms.TextInput(attrs={'class': 'form_widget'}), 'email': forms.TextInput(attrs={'class': 'form_widget'}), 'jabber': forms.TextInput(attrs={'class': 'form_widget'}), 'skype': forms.TextInput(attrs={'class': 'form_widget'}), 'contacts': forms.Textarea(attrs={'class': 'form_widget'}), 'bio': forms.Textarea(attrs={'class': 'form_widget'}), } birthday = forms.DateField(widget=DatePickerWidget( params="dateFormat: 'yy-mm-dd',\ changeYear: true,\ yearRange: 'c-115:c'", attrs={'class': 'datepicker form_widget'} ) ) def clean_birthday(self): birthday = self.cleaned_data['birthday'] if birthday < date(1900, 1, 1): raise forms.ValidationError("Valid years: 1900-2016.") if birthday > datetime.now().date(): raise forms.ValidationError("Date is older than today.") return birthday
def ModelFormFactory(some_model, *args, **kwargs): """ Create a ModelForm for `some_model`. `DateField` and `coloree.fields.HtmlColorCodeField` get their own widgets assigned, if there’s NO keyword argument 'disabled'. see also http://stackoverflow.com/questions/297383/dynamically-update-modelforms-meta-class """ widdict = {} if not ('disabled' in kwargs and kwargs['disabled']): # set some widgets for special fields for field in some_model._meta.local_fields: if type(field) is models.DateField: widdict[field.name] = DatePickerWidget() elif coloree_active and type(field) is HtmlColorCodeField: widdict[field.name] = ColorPickerWidget() try: del kwargs['disabled'] except KeyError: pass class MyModelForm(DorsaleBaseModelForm): class Meta: model = some_model widgets = widdict return MyModelForm(*args, **kwargs)
class Meta: model = Contact widgets = { 'date_of_birth': DatePickerWidget( params="dateFormat: 'yy-mm-dd', changeYear: true, " "defaultDate: '-28y', yearRange: 'c-60:c+15'", attrs={'class': 'datepicker'}), }
def __init__(self, *args, **kwargs): super(MyInfoForm, self).__init__(*args, **kwargs) self.fields['my_photo'].widget.attrs["onchange"] = "upload_img(this);" self.fields['birth_date'].widget = DatePickerWidget( params="dateFormat: 'yy-mm-dd', changeYear: true," " defaultDate: '-16y',yearRange: 'c-40:c+16'", attrs={ 'class': 'datepicker', })
def test_date_widget(self): self.assertTrue("""<script> $(function() { var pickerOpts = { dateFormat: "yy-mm-dd", showOtherMonths: true} $(".datepicker").datepicker(pickerOpts); });</script> <input id="id_test" name="test" class="datepicker" type="text" value="1989-07-07"/>""", DatePickerWidget().render('test', "1989-07-07"))
class AddPersonForm(forms.ModelForm): b_date = forms.DateField(widget=DatePickerWidget()) about = forms.CharField(widget=forms.Textarea) class Meta: model = Person fields = ( 'first_name', 'last_name', 'b_date', 'photography', 'email', 'jabber', 'about', )
class ProfileForm(forms.ModelForm): id = forms.IntegerField(widget=forms.HiddenInput()) name = forms.CharField( max_length=100, min_length=3, widget=forms.TextInput(attrs={'class': 'form-control'})) last_name = forms.CharField( max_length=100, min_length=3, widget=forms.TextInput(attrs={'class': 'form-control'})) date_of_birth = forms.DateField(widget=DatePickerWidget( params="dateFormat: 'yy-mm-dd', changeYear: true," " defaultDate: 'c-25', yearRange: 'c-115:c'", attrs={'class': 'datepicker'})) photo = forms.ImageField(required=False, widget=forms.FileInput) email = forms.EmailField( max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'})) jabber = forms.EmailField( max_length=100, min_length=3, widget=forms.TextInput(attrs={'class': 'form-control'})) skype = forms.CharField( max_length=100, min_length=3, widget=forms.TextInput(attrs={'class': 'form-control'})) other_contacts = forms.CharField( max_length=1000, required=False, widget=forms.Textarea(attrs={'class': 'form-control'})) bio = forms.CharField( max_length=1000, min_length=3, widget=forms.Textarea(attrs={'class': 'form-control'})) class Meta: model = Profile fields = [ 'id', 'name', 'last_name', 'date_of_birth', 'photo', 'bio', 'email', 'jabber', 'skype', 'other_contacts' ]