mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
newforms-admin: Merged to [5194]
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@5195 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -6,6 +6,7 @@ from formsets import formset_tests
|
||||
form_tests = r"""
|
||||
>>> from django.newforms import *
|
||||
>>> import datetime
|
||||
>>> import time
|
||||
>>> import re
|
||||
|
||||
###########
|
||||
@@ -659,10 +660,31 @@ Traceback (most recent call last):
|
||||
...
|
||||
IndexError: list index out of range
|
||||
|
||||
# Unicode choices are correctly rendered as HTML
|
||||
>>> w = RadioSelect()
|
||||
>>> unicode(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]))
|
||||
u'<ul>\n<li><label><input checked="checked" type="radio" name="email" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="radio" name="email" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>'
|
||||
|
||||
# Attributes provided at instantiation are passed to the constituent inputs
|
||||
>>> w = RadioSelect(attrs={'id':'foo'})
|
||||
>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
|
||||
<ul>
|
||||
<li><label><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
|
||||
<li><label><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
|
||||
<li><label><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
|
||||
<li><label><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
|
||||
</ul>
|
||||
|
||||
# Attributes provided at render-time are passed to the constituent inputs
|
||||
>>> w = RadioSelect()
|
||||
>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'})
|
||||
<ul>
|
||||
<li><label><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
|
||||
<li><label><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
|
||||
<li><label><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>
|
||||
<li><label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
|
||||
</ul>
|
||||
|
||||
# CheckboxSelectMultiple Widget ###############################################
|
||||
|
||||
>>> w = CheckboxSelectMultiple()
|
||||
@@ -784,6 +806,11 @@ u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li
|
||||
u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />'
|
||||
>>> w.render('name', 'john__lennon')
|
||||
u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />'
|
||||
>>> w.render('name', 'john__lennon', attrs={'id':'foo'})
|
||||
u'<input id="foo_0" type="text" class="big" value="john" name="name_0" /><br /><input id="foo_1" type="text" class="small" value="lennon" name="name_1" />'
|
||||
>>> w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})), attrs={'id': 'bar'})
|
||||
>>> w.render('name', ['john', 'lennon'])
|
||||
u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />'
|
||||
|
||||
# SplitDateTimeWidget #########################################################
|
||||
|
||||
@@ -2575,6 +2602,27 @@ underscores converted to spaces, and the initial letter capitalized.
|
||||
<li>Password1: <input type="password" name="password1" /></li>
|
||||
<li>Password (again): <input type="password" name="password2" /></li>
|
||||
|
||||
Labels for as_* methods will only end in a colon if they don't end in other
|
||||
punctuation already.
|
||||
>>> class Questions(Form):
|
||||
... q1 = CharField(label='The first question')
|
||||
... q2 = CharField(label='What is your name?')
|
||||
... q3 = CharField(label='The answer to life is:')
|
||||
... q4 = CharField(label='Answer this question!')
|
||||
... q5 = CharField(label='The last question. Period.')
|
||||
>>> print Questions(auto_id=False).as_p()
|
||||
<p>The first question: <input type="text" name="q1" /></p>
|
||||
<p>What is your name? <input type="text" name="q2" /></p>
|
||||
<p>The answer to life is: <input type="text" name="q3" /></p>
|
||||
<p>Answer this question! <input type="text" name="q4" /></p>
|
||||
<p>The last question. Period. <input type="text" name="q5" /></p>
|
||||
>>> print Questions().as_p()
|
||||
<p><label for="id_q1">The first question:</label> <input type="text" name="q1" id="id_q1" /></p>
|
||||
<p><label for="id_q2">What is your name?</label> <input type="text" name="q2" id="id_q2" /></p>
|
||||
<p><label for="id_q3">The answer to life is:</label> <input type="text" name="q3" id="id_q3" /></p>
|
||||
<p><label for="id_q4">Answer this question!</label> <input type="text" name="q4" id="id_q4" /></p>
|
||||
<p><label for="id_q5">The last question. Period.</label> <input type="text" name="q5" id="id_q5" /></p>
|
||||
|
||||
A label can be a Unicode object or a bytestring with special characters.
|
||||
>>> class UserRegistration(Form):
|
||||
... username = CharField(max_length=10, label='ŠĐĆŽćžšđ')
|
||||
@@ -3272,6 +3320,94 @@ True
|
||||
<option value="2016">2016</option>
|
||||
</select>
|
||||
|
||||
# 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')
|
||||
<input type="text" name="name_0" value="some text" />
|
||||
<select multiple="multiple" name="name_1">
|
||||
<option value="J" selected="selected">John</option>
|
||||
<option value="P" selected="selected">Paul</option>
|
||||
<option value="G">George</option>
|
||||
<option value="R">Ringo</option>
|
||||
</select>
|
||||
<input type="text" name="name_2_0" value="2007-04-25" /><input type="text" name="name_2_1" value="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
|
||||
<tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" />
|
||||
<select multiple="multiple" name="field1_1" id="id_field1_1">
|
||||
<option value="J">John</option>
|
||||
<option value="P">Paul</option>
|
||||
<option value="G">George</option>
|
||||
<option value="R">Ringo</option>
|
||||
</select>
|
||||
<input type="text" name="field1_2_0" id="id_field1_2_0" /><input type="text" name="field1_2_1" id="id_field1_2_1" /></td></tr>
|
||||
|
||||
>>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'})
|
||||
>>> print f
|
||||
<tr><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" />
|
||||
<select multiple="multiple" name="field1_1" id="id_field1_1">
|
||||
<option value="J" selected="selected">John</option>
|
||||
<option value="P" selected="selected">Paul</option>
|
||||
<option value="G">George</option>
|
||||
<option value="R">Ringo</option>
|
||||
</select>
|
||||
<input type="text" name="field1_2_0" value="2007-04-25" id="id_field1_2_0" /><input type="text" name="field1_2_1" value="06:24:00" id="id_field1_2_1" /></td></tr>
|
||||
|
||||
>>> f.clean_data
|
||||
{'field1': u'some text,JP,2007-04-25 06:24:00'}
|
||||
|
||||
#################################
|
||||
# Tests of underlying functions #
|
||||
#################################
|
||||
|
||||
Reference in New Issue
Block a user