Opened 9 years ago

Closed 9 years ago

#26234 closed New feature (wontfix)

Add `css_classes` parameter to Form.as_*() methods

Reported by: Will Stott Owned by: nobody
Component: Forms Version: 1.9
Severity: Normal Keywords: css forms
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Currently the as_p as_table and as_ul functions on forms have no customisation for the class of fields. Proposing adding the ability to pass css classes through. Personally this will be useful for semantic-ui integration, which expects the field's container to have the 'field' class to render properly.

Attachments (1)

django-forms-css.patch (3.3 KB ) - added by Will Stott 9 years ago.

Download all attachments as: .zip

Change History (5)

by Will Stott, 9 years ago

Attachment: django-forms-css.patch added

comment:1 by Tim Graham, 9 years ago

Summary: No way to specify css classes of form fields.Add `css_classes` parameter to Form.as_*() methods

I'm concerned that adding this could open a can worms of customization parameters for the as_*() methods which I think are really meant as simple shortcuts when very little customization is needed .

I'm inclined to say you should do this with a Python mixin:

class FormFieldClassesMixin(object):
    def __init__(self, *args, **kwargs):
        super(FormFieldClassesMixin, self).__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
            field.widget.attrs['class'] = 'someClass'

or in your own custom template tag. See also ideas on stackoverflow.

comment:2 by Will Stott, 9 years ago

Replying to timgraham:

I'm concerned that adding this could open a can worms of customization parameters for the as_*() methods which I think are really meant as simple shortcuts when very little customization is needed .

I understand why you're concerned. I would justify this by saying that the patch only modifies the actual tag specified in the method name. As the method is already a direct reference to that html tag, any customisation would be logically kept to the tag which that function adds (surely never more than attrs={}).

I also don't see many situations where additional parameters would get passed into the Field.css_classes method. As it would surely be only called in:

  • a template (where you would just class="{{field.css_classes}} myclass" anyway)
  • this Form._html_output method
  • or something very similar and unnecessary to write.

I just feel the .as_* methods are almost useless without some customisation. And it feels natural for me to do form.as_p('large') or the like with that method.

I'm inclined to say you should do this with a Python mixin:

Your particular snippet adds classes to the widget, not the form container (i.e. also encompassing the <label>).

But I get where you're coming from. And I've used a more granual(?) form template to get around this, which was simpler than I expected (but brings up the "what's the point" in the methods if they can't be adjusted argument again).

Last edited 9 years ago by Will Stott (previous) (diff)

in reply to:  1 comment:3 by Will Stott, 9 years ago

Replying to timgraham:

BTW this is my first time on trac, that comment above wasn't a reply to you at first, but I edited it to be. IDK if the notification would be sent on an edit. So I'm making this reply too.

comment:4 by Tim Graham, 9 years ago

Resolution: wontfix
Status: newclosed

Let's reopen this if you can get consensus on the DevelopersMailingList to do it. I'm still not certain promoting more use of these methods is a good idea.

Maybe template-based widget rendering (#15667) will help for your use case.

Note: See TracTickets for help on using tickets.
Back to Top