class SejourForm(SejourFormBase, ModelForm): date_arrivee = SplitDateTimeField( required=True, widget=SplitDateTimeWidget(time_format=('%H:%M'))) date_depart = SplitDateTimeField( required=True, widget=SplitDateTimeWidget(time_format=('%H:%M'))) class Meta: model = Sejour fields = ( "cohabitation", "date_arrivee", "date_depart", "proprietaire", "animaux", "nb_cages_fournies", "nb_cages_a_fournir", "vaccination", "soin", "injection", "commentaire", "montant", "arrhes", "montant_restant", ) widgets = { 'proprietaire': autocomplete.ModelSelect2(url='proprietaire_autocomplete') }
class CurrentFilter(django_filters.FilterSet): datetimeval = django_filters.DateTimeFromToRangeFilter(widget=SplitDateTimeWidget()) datetimeval = django_filters.DateTimeFromToRangeFilter(widget=SplitDateTimeWidget(attrs={'type': 'date'})) datetimeval.label = 'Διάστημα' class Meta: model = ModelClassName exclude = ('id',) fields = { 'datetimeval' : [], 'country' : ['exact', ], }
def test_constructor_different_attrs(self): html = ( '<input type="text" class="foo" value="2006-01-10" name="date_0" />' '<input type="text" class="bar" value="07:30:00" name="date_1" />' ) widget = SplitDateTimeWidget(date_attrs={'class': 'foo'}, time_attrs={'class': 'bar'}) self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html) widget = SplitDateTimeWidget(date_attrs={'class': 'foo'}, attrs={'class': 'bar'}) self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html) widget = SplitDateTimeWidget(time_attrs={'class': 'bar'}, attrs={'class': 'foo'}) self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html)
class CurrentFilter(django_filters.FilterSet): datetime = django_filters.DateTimeFromToRangeFilter(widget=SplitDateTimeWidget()) datetime = django_filters.DateTimeFromToRangeFilter(widget=SplitDateTimeWidget(attrs={'type': 'date'})) datetime.label = 'Διάστημα' class Meta: model = ModelClassName exclude = ('id',) fields = { 'datetime': ['exact', ], 'task': ['exact', ], 'scheduled_task': ['exact', ], 'status': ['exact', ],}
def __init__(self, attrs=None): widgets = ( TextInput(), SelectMultiple(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), SplitDateTimeWidget(), ) super(ComplexMultiWidget, self).__init__(widgets, attrs)
class StoryForm(ModelForm): pub_date = SplitDateTimeField( widget=SplitDateTimeWidget( date_attrs={'type': 'date'}, time_attrs={'type': 'time'}, ), label="Published on" ) class Meta: model = NewsStory fields = ['title', 'pub_date', 'image', 'story_category', 'content'] labels = {'image': "Image URL"} widgets = { 'pub_date': forms.DateInput( format = ('%m/%d/%Y'), attrs={ 'class': 'form-control', 'placeholder': 'Select a date', 'type': 'date' } ), 'category': forms.ModelMultipleChoiceField( widget = forms.SelectMultiple, queryset = Category.objects.all() ), }
class StoryForm(ModelForm): image_link = forms.URLField(required=False) # ,help_text='Enter URL to a direct image. Otherwise, a random image will be chosen for you') pub_date = SplitDateTimeField( # use split date time field to allow the user to input both date and time widget=SplitDateTimeWidget( # 'class'="form-item", # we use the split date time widget to specify how the html gets built date_attrs={ 'type': 'date', 'class': "form-item", 'id': 'form-date' }, # type date tells django to use the HTML5 date input time_attrs={ 'type': 'time', 'class': "form-item", 'id': 'form-time' }, # type time tells django to use the HTML5 time input # default=timezone.now )) class Meta: model = NewsStory fields = ['title', 'pub_date', 'content', 'image_link', 'category'] widgets = { 'title': forms.TextInput( attrs={ 'size': 10, 'id': 'form-title', 'class': "form-item", 'placeholder': 'Enter a catchy title', }), # 'author': forms.TextInput(attrs={'size': 10, 'id': 'form-author','class': "form-item",'placeholder': 'Your name',} # ), 'content': forms.Textarea( attrs={ 'size': 10, 'id': 'form-content', 'class': "form-item", 'placeholder': 'Enter a rivoting story', }), 'image_link': forms.TextInput(attrs={ 'size': 10, 'id': 'form-link', 'class': "form-item", 'placeholder': 'Enter URL to a direct image. Otherwise, a random image will be chosen for you', }, ), # forces you to upload with url # want to verify it exists. Django automatically does this }
def __init__(self, attrs=None): widgets = ( TextInput(), SelectMultiple(choices=beatles), SplitDateTimeWidget(), ) super(ComplexMultiWidget, self).__init__(widgets, attrs)
class StoryForm(ModelForm): pub_date = SplitDateTimeField(widget=SplitDateTimeWidget( date_attrs={'type': 'date'}, time_attrs={'type': 'time'}, ), label='Publication Date') class Meta: model = NewsStory fields = ['title', 'pub_date', 'image', 'content'] labels = {'image': 'Image URL'} widgets = { 'title': forms.TextInput(attrs={ 'class': 'form', 'placeholder': 'Article Title', }), 'content': forms.Textarea( attrs={ 'class': 'form', 'id': 'box-size', 'placeholder': 'Article Content' }), 'image': forms.URLInput(attrs={ 'class': 'form', 'placeholder': 'Enter image URL here' }), }
def __init__(self, attrs=None): widgets = ( TextInput(), SelectMultiple(choices=WidgetTest.beatles), SplitDateTimeWidget(), ) super().__init__(widgets, attrs)
def test_formatting(self): """ Use 'date_format' and 'time_format' to change the way a value is displayed. """ widget = SplitDateTimeWidget( date_format="%d/%m/%Y", time_format="%H:%M", ) self.check_html( widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="10/01/2006">' '<input type="text" name="date_1" value="07:30">' ), ) self.check_html( widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="10/01/2006">' '<input type="text" name="date_1" value="07:30">' ), )
class SplitDateTimeWidgetTest(WidgetTest): widget = SplitDateTimeWidget() def test_render_empty(self): self.check_html(self.widget, 'date', '', html=( '<input type="text" name="date_0" /><input type="text" name="date_1" />' )) def test_render_none(self): self.check_html(self.widget, 'date', None, html=( '<input type="text" name="date_0" /><input type="text" name="date_1" />' )) def test_render_datetime(self): self.check_html(self.widget, 'date', datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="2006-01-10" />' '<input type="text" name="date_1" value="07:30:00" />' )) def test_render_date_and_time(self): self.check_html(self.widget, 'date', [date(2006, 1, 10), time(7, 30)], html=( '<input type="text" name="date_0" value="2006-01-10" />' '<input type="text" name="date_1" value="07:30:00" />' )) def test_constructor_attrs(self): widget = SplitDateTimeWidget(attrs={'class': 'pretty'}) self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=( '<input type="text" class="pretty" value="2006-01-10" name="date_0" />' '<input type="text" class="pretty" value="07:30:00" name="date_1" />' )) def test_constructor_different_attrs(self): html = ( '<input type="text" class="foo" value="2006-01-10" name="date_0" />' '<input type="text" class="bar" value="07:30:00" name="date_1" />' ) widget = SplitDateTimeWidget(date_attrs={'class': 'foo'}, time_attrs={'class': 'bar'}) self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html) widget = SplitDateTimeWidget(date_attrs={'class': 'foo'}, attrs={'class': 'bar'}) self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html) widget = SplitDateTimeWidget(time_attrs={'class': 'bar'}, attrs={'class': 'foo'}) self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html) def test_formatting(self): """ Use 'date_format' and 'time_format' to change the way a value is displayed. """ widget = SplitDateTimeWidget( date_format='%d/%m/%Y', time_format='%H:%M', ) self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="10/01/2006" />' '<input type="text" name="date_1" value="07:30" />' ))
def test_constructor_attrs(self): widget = SplitDateTimeWidget(attrs={'class': 'pretty'}) self.check_html( widget, 'date', datetime(2006, 1, 10, 7, 30), html= ('<input type="text" class="pretty" value="2006-01-10" name="date_0">' '<input type="text" class="pretty" value="07:30:00" name="date_1">' ))
class StoryForm(ModelForm): Cuisine_CHOICES = [ ('French', 'French'), ('Italian', 'Italian'), ('Modern Australian', 'Modern Australian'), ('Indonesian', 'Indonesian'), ('Chinese', 'Chinese'), ('Japanese', 'Japanese'), ('Thai', 'Thai'), ('American', 'American'), ('Middle Eastern', 'Middle Eastern'), ('Indian', 'Indian'), ('Moroccan', 'Moroccan'), ('Other', 'Other'), ] image_upload = forms.URLField(required=False, help_text='If image path is left blank, a randomly generated image will appear in your article', widget=forms.TextInput(attrs={'class': "form_entry"})) CHOICES = [('1','Awful'),('2','Average'),('3','Okay'),('4','Good'),('5','Fantastic')] Food_rating=forms.CharField(label='Overall Experience Rating', widget=forms.RadioSelect(choices=CHOICES)) pub_date = SplitDateTimeField( # use split date time field to allow the user to input both date and time widget=SplitDateTimeWidget( # we use the split date time widget to specify how the html gets built date_attrs={'type': 'date', 'class':'form_entry'}, # type date tells django to use the HTML5 date input time_attrs={'type': 'time','class':'form_entry'}, # type time tells django to use the HTML5 time input ) ) class Meta: model = NewsStory fields = ['title','Restaurant', 'pub_date','image_upload','content','cuisine_type'] widgets = { "content": forms.Textarea( attrs={'placeholder': 'We cannot wait to hear where you have eaten and what it was like!','class':'form_entry' }, ) , "title": forms.TextInput( attrs={ 'class':'form_entry'} ) , "Restaurant": forms.TextInput( attrs={ 'class':'form_entry'} ) }
class StoryForm(ModelForm): class Meta: model=NewsStory fields= ['title', 'pub_date', 'content', 'img_url'] pub_date = SplitDateTimeField( # use split date time field to allow the user to input both date and time widget=SplitDateTimeWidget( # we use the split date time widget to specify how the html gets built date_attrs={'type': 'date'}, # type date tells django to use the HTML5 date input time_attrs={'type': 'time'}, # type time tells django to use the HTML5 time input ) )
class StoryForm(ModelForm): pub_date = SplitDateTimeField( widget = SplitDateTimeWidget( date_attrs={'type': 'date'}, time_attrs={'type': 'time'}, ) ) class Meta: model = NewsStory fields = ['title', 'pub_date', 'content', 'image'] labels = { 'image' : _('Image (URL)'), } widgets = { 'title' : forms.TextInput(attrs={'placeholder': 'Title'}), 'content' : forms.Textarea(attrs={'placeholder': 'Story Content'}), 'image' : forms.URLInput(attrs={'placeholder': 'Image URL'}) }
class Meta: model = Entry fields = ( 'title', 'slug', 'author', 'image', 'image_caption', 'content', 'excerpt', 'status', 'template', 'start_publication', ) widgets = { 'content': CKEditorWidget(), 'excerpt': CKEditorWidget(config_name='basic_toolbar'), 'start_publication': SplitDateTimeWidget(), }
class CurrentFilter(django_filters.FilterSet): #datetimeval = django_filters.DateTimeFromToRangeFilter(widget=SplitDateTimeWidget()) datetimeval = django_filters.DateTimeFromToRangeFilter( widget=SplitDateTimeWidget(attrs={'type': 'date'})) datetimeval.label = 'Διάστημα ' status_ok = django_filters.BooleanFilter( widget=django_filters.widgets.BooleanWidget()) #country_id = django_filters.NumberFilter(method='country_id') ## aaa=Country.objects.filter(countryft=1) ## ccc=[] ## for x in aaa.values(): ## print(x['id'],x['name']) ## ccc.append((int(x['id']),x['name'].strip())) # country_id = django_filters.MultipleChoiceFilter(choices=ccc) class Meta: model = ModelClassName exclude = ('id', ) fields = { 'datetimeval': [], 'metric_id': [ 'exact', ], 'metric_id__metric_category': [ 'exact', ], 'country_id': [ 'exact', ], ## 'country_id': [], 'status_ok': [], }
class StoryForm(ModelForm): pub_date = SplitDateTimeField(widget=SplitDateTimeWidget( date_attrs={'type': 'date'}, time_attrs={'type': 'time'}, )) class Meta: model = NewsStory fields = ['title', 'pub_date', 'content', 'image'] widgets = { 'title': forms.TextInput(attrs={ 'class': 'form', 'class': 'inputbox', 'placeholder': 'Title Name' }), 'content': forms.Textarea( attrs={ 'class': 'form', 'class': 'inputbox', 'placeholder': 'Enter Content Here' }) }
class SplitDateTimeWidgetTest(WidgetTest): widget = SplitDateTimeWidget() def test_render_empty(self): self.check_html( self.widget, "date", "", html=('<input type="text" name="date_0"><input type="text" name="date_1">'), ) def test_render_none(self): self.check_html( self.widget, "date", None, html=('<input type="text" name="date_0"><input type="text" name="date_1">'), ) def test_render_datetime(self): self.check_html( self.widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="2006-01-10">' '<input type="text" name="date_1" value="07:30:00">' ), ) def test_render_date_and_time(self): self.check_html( self.widget, "date", [date(2006, 1, 10), time(7, 30)], html=( '<input type="text" name="date_0" value="2006-01-10">' '<input type="text" name="date_1" value="07:30:00">' ), ) def test_constructor_attrs(self): widget = SplitDateTimeWidget(attrs={"class": "pretty"}) self.check_html( widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" class="pretty" value="2006-01-10" name="date_0">' '<input type="text" class="pretty" value="07:30:00" name="date_1">' ), ) def test_constructor_different_attrs(self): html = ( '<input type="text" class="foo" value="2006-01-10" name="date_0">' '<input type="text" class="bar" value="07:30:00" name="date_1">' ) widget = SplitDateTimeWidget( date_attrs={"class": "foo"}, time_attrs={"class": "bar"} ) self.check_html(widget, "date", datetime(2006, 1, 10, 7, 30), html=html) widget = SplitDateTimeWidget( date_attrs={"class": "foo"}, attrs={"class": "bar"} ) self.check_html(widget, "date", datetime(2006, 1, 10, 7, 30), html=html) widget = SplitDateTimeWidget( time_attrs={"class": "bar"}, attrs={"class": "foo"} ) self.check_html(widget, "date", datetime(2006, 1, 10, 7, 30), html=html) def test_formatting(self): """ Use 'date_format' and 'time_format' to change the way a value is displayed. """ widget = SplitDateTimeWidget( date_format="%d/%m/%Y", time_format="%H:%M", ) self.check_html( widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="10/01/2006">' '<input type="text" name="date_1" value="07:30">' ), )
class SplitDateTimeWidgetTest(WidgetTest): widget = SplitDateTimeWidget() def test_render_empty(self): self.check_html( self.widget, "date", "", html=('<input type="text" name="date_0"><input type="text" name="date_1">'), ) def test_render_none(self): self.check_html( self.widget, "date", None, html=('<input type="text" name="date_0"><input type="text" name="date_1">'), ) def test_render_datetime(self): self.check_html( self.widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="2006-01-10">' '<input type="text" name="date_1" value="07:30:00">' ), ) def test_render_date_and_time(self): self.check_html( self.widget, "date", [date(2006, 1, 10), time(7, 30)], html=( '<input type="text" name="date_0" value="2006-01-10">' '<input type="text" name="date_1" value="07:30:00">' ), ) def test_constructor_attrs(self): widget = SplitDateTimeWidget(attrs={"class": "pretty"}) self.check_html( widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" class="pretty" value="2006-01-10" name="date_0">' '<input type="text" class="pretty" value="07:30:00" name="date_1">' ), ) def test_constructor_different_attrs(self): html = ( '<input type="text" class="foo" value="2006-01-10" name="date_0">' '<input type="text" class="bar" value="07:30:00" name="date_1">' ) widget = SplitDateTimeWidget( date_attrs={"class": "foo"}, time_attrs={"class": "bar"} ) self.check_html(widget, "date", datetime(2006, 1, 10, 7, 30), html=html) widget = SplitDateTimeWidget( date_attrs={"class": "foo"}, attrs={"class": "bar"} ) self.check_html(widget, "date", datetime(2006, 1, 10, 7, 30), html=html) widget = SplitDateTimeWidget( time_attrs={"class": "bar"}, attrs={"class": "foo"} ) self.check_html(widget, "date", datetime(2006, 1, 10, 7, 30), html=html) def test_formatting(self): """ Use 'date_format' and 'time_format' to change the way a value is displayed. """ widget = SplitDateTimeWidget( date_format="%d/%m/%Y", time_format="%H:%M", ) self.check_html( widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="10/01/2006">' '<input type="text" name="date_1" value="07:30">' ), ) self.check_html( widget, "date", datetime(2006, 1, 10, 7, 30), html=( '<input type="text" name="date_0" value="10/01/2006">' '<input type="text" name="date_1" value="07:30">' ), ) def test_fieldset(self): class TestForm(Form): template_name = "forms_tests/use_fieldset.html" field = SplitDateTimeField(widget=self.widget) form = TestForm() self.assertIs(self.widget.use_fieldset, True) self.assertHTMLEqual( '<div><fieldset><legend>Field:</legend><input type="text" ' 'name="field_0" required id="id_field_0"><input type="text" ' 'name="field_1" required id="id_field_1"></fieldset></div>', form.render(), )
class EventForm(ModelForm): date = SplitDateTimeField(label='Termin spotkania', widget=SplitDateTimeWidget(date_attrs={ 'type': 'text', 'class': 'datepicker', 'placeholder': 'np. 01-01-2018' }, time_attrs={ 'type': 'text', 'class': 'timepicker', 'placeholder': 'np. 20:30' }), input_date_formats=settings.DATE_INPUT_FORMATS) tags = ModelCommaSeparatedChoiceField( label='Tagi', queryset=Tag.objects.all(), to_field_name='name', required=False, help_text= "Dodaj tagi, aby Twoje spotknie było łatwiejsze do odnalezienia. Każdy tag musi być oddzielony od kolejnego przecinkiem (np. piłka nożna, hala)." ) class Meta: model = Event fields = [ 'title', 'category', 'city', 'city_area', 'min_number_of_participants', 'max_number_of_participants', 'description', ] labels = { 'title': 'Tytuł spotkania', 'category': 'Kategoria', 'city': 'Miasto', 'city_area': 'Obszar miasta', 'min_number_of_participants': 'Minimalna liczba uczestników', 'max_number_of_participants': 'Maksymalna liczba uczestników', 'description': 'Opis spotkania', } widgets = { 'category': RadioSelect(), 'description': Textarea(attrs={'class': 'materialize-textarea'}) } def __init__(self, *args, **kwargs): super(EventForm, self).__init__(*args, **kwargs) instance = getattr(self, 'instance', None) if instance and instance.pk: self.fields['title'].disabled = True self.fields['category'].disabled = True, self.fields['city'].disabled = True, self.fields['description'].disabled = True, self.fields['tags'].disabled = True, def clean_date(self): date = self.cleaned_data['date'] if date < timezone.now() + timezone.timedelta(minutes=30): raise ValidationError( 'Nie możesz stworzyć spotkania, do rozpoczęcia którego zostało mniej niż 30 minut.' ) return date def clean(self): cleaned_data = super(EventForm, self).clean() if cleaned_data['min_number_of_participants'] and cleaned_data[ 'min_number_of_participants'] > cleaned_data[ 'max_number_of_participants']: raise ValidationError( 'Minimalna liczba uczestników musi być mniejsza od maksymalnej liczby uczestników spotkania.' ) return cleaned_data