Fixed #3929 -- Newforms Textarea widget now always includes 'rows' and 'cols' attributes, even if you don't pass them in, for HTML correctness. Thanks, and welcome back, Luke Plant
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4961 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
7693000788
commit
e34e433641
|
@ -121,6 +121,12 @@ class FileInput(Input):
|
|||
input_type = 'file'
|
||||
|
||||
class Textarea(Widget):
|
||||
def __init__(self, attrs=None):
|
||||
# The 'rows' and 'cols' attributes are required for HTML correctness.
|
||||
self.attrs = {'cols': '40', 'rows': '10'}
|
||||
if attrs:
|
||||
self.attrs.update(attrs)
|
||||
|
||||
def render(self, name, value, attrs=None):
|
||||
if value is None: value = ''
|
||||
value = smart_unicode(value)
|
||||
|
|
|
@ -159,7 +159,7 @@ represented by a ChoiceField.
|
|||
<option value="1">Mike Royko</option>
|
||||
<option value="2">Bob Woodward</option>
|
||||
</select></td></tr>
|
||||
<tr><th>Article:</th><td><textarea name="article"></textarea></td></tr>
|
||||
<tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr>
|
||||
<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
|
||||
<option value="1">Entertainment</option>
|
||||
<option value="2">It's a test</option>
|
||||
|
@ -199,7 +199,7 @@ current values are inserted as 'initial' data in each Field.
|
|||
<option value="1" selected="selected">Mike Royko</option>
|
||||
<option value="2">Bob Woodward</option>
|
||||
</select></li>
|
||||
<li>Article: <textarea name="article">Hello.</textarea></li>
|
||||
<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
|
||||
<li>Categories: <select multiple="multiple" name="categories">
|
||||
<option value="1">Entertainment</option>
|
||||
<option value="2">It's a test</option>
|
||||
|
@ -231,7 +231,7 @@ Add some categories and test the many-to-many form output.
|
|||
<option value="1" selected="selected">Mike Royko</option>
|
||||
<option value="2">Bob Woodward</option>
|
||||
</select></li>
|
||||
<li>Article: <textarea name="article">Hello.</textarea></li>
|
||||
<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
|
||||
<li>Categories: <select multiple="multiple" name="categories">
|
||||
<option value="1" selected="selected">Entertainment</option>
|
||||
<option value="2">It's a test</option>
|
||||
|
@ -309,7 +309,7 @@ the data in the database when the form is instantiated.
|
|||
<option value="1">Mike Royko</option>
|
||||
<option value="2">Bob Woodward</option>
|
||||
</select></li>
|
||||
<li>Article: <textarea name="article"></textarea></li>
|
||||
<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
|
||||
<li>Categories: <select multiple="multiple" name="categories">
|
||||
<option value="1">Entertainment</option>
|
||||
<option value="2">It's a test</option>
|
||||
|
@ -328,7 +328,7 @@ the data in the database when the form is instantiated.
|
|||
<option value="2">Bob Woodward</option>
|
||||
<option value="3">Carl Bernstein</option>
|
||||
</select></li>
|
||||
<li>Article: <textarea name="article"></textarea></li>
|
||||
<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
|
||||
<li>Categories: <select multiple="multiple" name="categories">
|
||||
<option value="1">Entertainment</option>
|
||||
<option value="2">It's a test</option>
|
||||
|
|
|
@ -193,30 +193,30 @@ u'<input type="file" class="fun" value="\u0160\u0110\u0106\u017d\u0107\u017e\u01
|
|||
|
||||
>>> w = Textarea()
|
||||
>>> w.render('msg', '')
|
||||
u'<textarea name="msg"></textarea>'
|
||||
u'<textarea rows="10" cols="40" name="msg"></textarea>'
|
||||
>>> w.render('msg', None)
|
||||
u'<textarea name="msg"></textarea>'
|
||||
u'<textarea rows="10" cols="40" name="msg"></textarea>'
|
||||
>>> w.render('msg', 'value')
|
||||
u'<textarea name="msg">value</textarea>'
|
||||
u'<textarea rows="10" cols="40" name="msg">value</textarea>'
|
||||
>>> w.render('msg', 'some "quoted" & ampersanded value')
|
||||
u'<textarea name="msg">some "quoted" & ampersanded value</textarea>'
|
||||
>>> w.render('msg', 'value', attrs={'class': 'pretty'})
|
||||
u'<textarea name="msg" class="pretty">value</textarea>'
|
||||
u'<textarea rows="10" cols="40" name="msg">some "quoted" & ampersanded value</textarea>'
|
||||
>>> w.render('msg', 'value', attrs={'class': 'pretty', 'rows': 20})
|
||||
u'<textarea class="pretty" rows="20" cols="40" name="msg">value</textarea>'
|
||||
|
||||
You can also pass 'attrs' to the constructor:
|
||||
>>> w = Textarea(attrs={'class': 'pretty'})
|
||||
>>> w.render('msg', '')
|
||||
u'<textarea class="pretty" name="msg"></textarea>'
|
||||
u'<textarea rows="10" cols="40" name="msg" class="pretty"></textarea>'
|
||||
>>> w.render('msg', 'example')
|
||||
u'<textarea class="pretty" name="msg">example</textarea>'
|
||||
u'<textarea rows="10" cols="40" name="msg" class="pretty">example</textarea>'
|
||||
|
||||
'attrs' passed to render() get precedence over those passed to the constructor:
|
||||
>>> w = Textarea(attrs={'class': 'pretty'})
|
||||
>>> w.render('msg', '', attrs={'class': 'special'})
|
||||
u'<textarea class="special" name="msg"></textarea>'
|
||||
u'<textarea rows="10" cols="40" name="msg" class="special"></textarea>'
|
||||
|
||||
>>> w.render('msg', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'})
|
||||
u'<textarea class="fun" name="msg">\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</textarea>'
|
||||
u'<textarea rows="10" cols="40" name="msg" class="fun">\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</textarea>'
|
||||
|
||||
# CheckboxInput Widget ########################################################
|
||||
|
||||
|
@ -1966,12 +1966,12 @@ Any Field can have a Widget class passed to its constructor:
|
|||
>>> print f['subject']
|
||||
<input type="text" name="subject" />
|
||||
>>> print f['message']
|
||||
<textarea name="message"></textarea>
|
||||
<textarea rows="10" cols="40" name="message"></textarea>
|
||||
|
||||
as_textarea(), as_text() and as_hidden() are shortcuts for changing the output
|
||||
widget type:
|
||||
>>> f['subject'].as_textarea()
|
||||
u'<textarea name="subject"></textarea>'
|
||||
u'<textarea rows="10" cols="40" name="subject"></textarea>'
|
||||
>>> f['message'].as_text()
|
||||
u'<input type="text" name="message" />'
|
||||
>>> f['message'].as_hidden()
|
||||
|
@ -1991,7 +1991,7 @@ as_hidden():
|
|||
u'<input type="text" name="message" />'
|
||||
>>> f = ContactForm({'subject': 'Hello', 'message': 'I love you.'}, auto_id=False)
|
||||
>>> f['subject'].as_textarea()
|
||||
u'<textarea name="subject">Hello</textarea>'
|
||||
u'<textarea rows="10" cols="40" name="subject">Hello</textarea>'
|
||||
>>> f['message'].as_text()
|
||||
u'<input type="text" name="message" value="I love you." />'
|
||||
>>> f['message'].as_hidden()
|
||||
|
|
Loading…
Reference in New Issue