diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 8d292673a5..f701faa35d 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -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) diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index d91f1d2d45..2757787571 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -159,7 +159,7 @@ represented by a ChoiceField. -Article: +Article: Categories: -
  • Article:
  • +
  • Article:
  • Categories:
  • -
  • Article:
  • +
  • Article:
  • Categories:
  • -
  • Article:
  • +
  • Article:
  • Categories:
  • -
  • Article:
  • +
  • Article:
  • Categories: ' +u'' >>> w.render('msg', None) -u'' +u'' >>> w.render('msg', 'value') -u'' +u'' >>> w.render('msg', 'some "quoted" & ampersanded value') -u'' ->>> w.render('msg', 'value', attrs={'class': 'pretty'}) -u'' +u'' +>>> w.render('msg', 'value', attrs={'class': 'pretty', 'rows': 20}) +u'' You can also pass 'attrs' to the constructor: >>> w = Textarea(attrs={'class': 'pretty'}) >>> w.render('msg', '') -u'' +u'' >>> w.render('msg', 'example') -u'' +u'' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = Textarea(attrs={'class': 'pretty'}) >>> w.render('msg', '', attrs={'class': 'special'}) -u'' +u'' >>> w.render('msg', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}) -u'' +u'' # CheckboxInput Widget ######################################################## @@ -1966,12 +1966,12 @@ Any Field can have a Widget class passed to its constructor: >>> print f['subject'] >>> print f['message'] - + as_textarea(), as_text() and as_hidden() are shortcuts for changing the output widget type: >>> f['subject'].as_textarea() -u'' +u'' >>> f['message'].as_text() u'' >>> f['message'].as_hidden() @@ -1991,7 +1991,7 @@ as_hidden(): u'' >>> f = ContactForm({'subject': 'Hello', 'message': 'I love you.'}, auto_id=False) >>> f['subject'].as_textarea() -u'' +u'' >>> f['message'].as_text() u'' >>> f['message'].as_hidden()