Ticket #26234: django-forms-css.patch

File django-forms-css.patch, 3.3 KB (added by Will Stott, 9 years ago)
  • django/forms/forms.py

    From 705f8513ebf9c1fa2b73390f34b1f217f05e2958 Mon Sep 17 00:00:00 2001
    From: Will Stott <willstott101@gmail.com>
    Date: Thu, 18 Feb 2016 12:32:36 +0000
    Subject: [PATCH] Pass extra_classes through from as_* functions.
    
    ---
     django/forms/forms.py | 19 +++++++++++--------
     1 file changed, 11 insertions(+), 8 deletions(-)
    
    diff --git a/django/forms/forms.py b/django/forms/forms.py
    index 7aceb90..c79bbd1 100644
    a b def add_initial_prefix(self, field_name):  
    175175        """
    176176        return 'initial-%s' % self.add_prefix(field_name)
    177177
    178     def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
     178    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row, extra_classes=None):
    179179        "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
    180180        top_errors = self.non_field_errors()  # Errors that should be displayed above all fields.
    181181        output, hidden_fields = [], []
    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_  
    194194            else:
    195195                # Create a 'class="..."' attribute if the row should have any
    196196                # CSS classes applied.
    197                 css_classes = bf.css_classes()
     197                css_classes = bf.css_classes(extra_classes=extra_classes)
    198198                if css_classes:
    199199                    html_class_attr = ' class="%s"' % css_classes
    200200
    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_  
    253253                output.append(str_hidden)
    254254        return mark_safe('\n'.join(output))
    255255
    256     def as_table(self):
     256    def as_table(self, extra_classes=None):
    257257        "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
    258258        return self._html_output(
    259259            normal_row='<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>',
    260260            error_row='<tr><td colspan="2">%s</td></tr>',
    261261            row_ender='</td></tr>',
    262262            help_text_html='<br /><span class="helptext">%s</span>',
    263             errors_on_separate_row=False)
     263            errors_on_separate_row=False,
     264            extra_classes=extra_classes)
    264265
    265     def as_ul(self):
     266    def as_ul(self, extra_classes=None):
    266267        "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
    267268        return self._html_output(
    268269            normal_row='<li%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</li>',
    269270            error_row='<li>%s</li>',
    270271            row_ender='</li>',
    271272            help_text_html=' <span class="helptext">%s</span>',
    272             errors_on_separate_row=False)
     273            errors_on_separate_row=False,
     274            extra_classes=extra_classes)
    273275
    274     def as_p(self):
     276    def as_p(self, extra_classes=None):
    275277        "Returns this form rendered as HTML <p>s."
    276278        return self._html_output(
    277279            normal_row='<p%(html_class_attr)s>%(label)s %(field)s%(help_text)s</p>',
    278280            error_row='%s',
    279281            row_ender='</p>',
    280282            help_text_html=' <span class="helptext">%s</span>',
    281             errors_on_separate_row=True)
     283            errors_on_separate_row=True,
     284            extra_classes=extra_classes)
    282285
    283286    def non_field_errors(self):
    284287        """
Back to Top