From e34e43364144127b8dcede61c3b5e12abb174e9a Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Mon, 9 Apr 2007 01:22:45 +0000 Subject: [PATCH] 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 --- django/newforms/widgets.py | 6 ++++++ tests/modeltests/model_forms/models.py | 10 +++++----- tests/regressiontests/forms/tests.py | 26 +++++++++++++------------- 3 files changed, 24 insertions(+), 18 deletions(-) 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()