# -*- coding: utf-8 -*- tests = r""" >>> from django.forms import * >>> from django.utils.encoding import force_unicode >>> import datetime >>> import time >>> import re >>> from decimal import Decimal ############### # Extra stuff # ############### The forms library comes with some extra, higher-level Field and Widget classes that demonstrate some of the library's abilities. # SelectDateWidget ############################################################ >>> from django.forms.extras import SelectDateWidget >>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016')) >>> print w.render('mydate', '') >>> w.render('mydate', None) == w.render('mydate', '') True >>> print w.render('mydate', '2010-04-15') Accepts a datetime or a string: >>> w.render('mydate', datetime.date(2010, 4, 15)) == w.render('mydate', '2010-04-15') True Invalid dates still render the failed date: >>> print w.render('mydate', '2010-02-31') Using a SelectDateWidget in a form: >>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'), required=False) >>> print w.render('mydate', '') >>> print w.render('mydate', '2010-04-15') >>> class GetDate(Form): ... mydate = DateField(widget=SelectDateWidget) >>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'}) >>> print a.is_valid() True >>> print a.cleaned_data['mydate'] 2008-04-01 As with any widget that implements get_value_from_datadict, we must be prepared to accept the input from the "as_hidden" rendering as well. >>> print a['mydate'].as_hidden() >>> b=GetDate({'mydate':'2008-4-1'}) >>> print b.is_valid() True >>> print b.cleaned_data['mydate'] 2008-04-01 USE_L10N tests >>> from django.utils import translation >>> translation.activate('nl') >>> from django.conf import settings >>> settings.USE_L10N=True >>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date') '13-08-2010' >>> print w.render('date', '13-08-2010') Years before 1900 work >>> w = SelectDateWidget(years=('1899',)) >>> w.value_from_datadict({'date_year': '1899', 'date_month': '8', 'date_day': '13'}, {}, 'date') '13-08-1899' >>> translation.deactivate() # MultiWidget and MultiValueField ############################################# # MultiWidgets are widgets composed of other widgets. They are usually # combined with MultiValueFields - a field that is composed of other fields. # MulitWidgets can themselved be composed of other MultiWidgets. # SplitDateTimeWidget is one example of a MultiWidget. >>> class ComplexMultiWidget(MultiWidget): ... def __init__(self, attrs=None): ... widgets = ( ... TextInput(), ... SelectMultiple(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), ... SplitDateTimeWidget(), ... ) ... super(ComplexMultiWidget, self).__init__(widgets, attrs) ... ... def decompress(self, value): ... if value: ... data = value.split(',') ... return [data[0], data[1], datetime.datetime(*time.strptime(data[2], "%Y-%m-%d %H:%M:%S")[0:6])] ... return [None, None, None] ... def format_output(self, rendered_widgets): ... return u'\n'.join(rendered_widgets) >>> w = ComplexMultiWidget() >>> print w.render('name', 'some text,JP,2007-04-25 06:24:00') >>> class ComplexField(MultiValueField): ... def __init__(self, required=True, widget=None, label=None, initial=None): ... fields = ( ... CharField(), ... MultipleChoiceField(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), ... SplitDateTimeField() ... ) ... super(ComplexField, self).__init__(fields, required, widget, label, initial) ... ... def compress(self, data_list): ... if data_list: ... return '%s,%s,%s' % (data_list[0],''.join(data_list[1]),data_list[2]) ... return None >>> f = ComplexField(widget=w) >>> f.clean(['some text', ['J','P'], ['2007-04-25','6:24:00']]) u'some text,JP,2007-04-25 06:24:00' >>> f.clean(['some text',['X'], ['2007-04-25','6:24:00']]) Traceback (most recent call last): ... ValidationError: [u'Select a valid choice. X is not one of the available choices.'] # If insufficient data is provided, None is substituted >>> f.clean(['some text',['JP']]) Traceback (most recent call last): ... ValidationError: [u'This field is required.'] >>> class ComplexFieldForm(Form): ... field1 = ComplexField(widget=w) >>> f = ComplexFieldForm() >>> print f
Name:
Email:
Comment:
################################# # Test multipart-encoded form # ################################# >>> class FormWithoutFile(Form): ... username = CharField() >>> class FormWithFile(Form): ... username = CharField() ... file = FileField() >>> class FormWithImage(Form): ... image = ImageField() >>> FormWithoutFile().is_multipart() False >>> FormWithFile().is_multipart() True >>> FormWithImage().is_multipart() True """