jazzband / django-floppyforms

Full control of form rendering in the templates.
http://django-floppyforms.readthedocs.org/
Other
841 stars 148 forks source link

Widgets not rendering extra attrs #202

Closed aquilante closed 4 years ago

aquilante commented 5 years ago

I'm working on Django 1.11.20 and floppyforms 1.7.0

I had a code like this:

import floppyforms as forms

class DataRequestForm(forms.ModelForm):
    date_start = forms.DateField(label='Start Date',
                                 input_formats=['%Y-%m-%d', '%d/%m/%Y', '%d/%m/%y'],
                                 help_text=_("The start date of the requested dataset"),
                                 widget=forms.TextInput(attrs={'class':'dateinput'}),
                                 )
    date_end = forms.DateField(label='End Date', 
                               input_formats=['%Y-%m-%d', '%d/%m/%Y', '%d/%m/%y'],
                               help_text=_("The end date of the requested dataset"),
                               widget=forms.TextInput(attrs={'class':'dateinput'}),
                               )
    countries = forms.ModelMultipleChoiceField(
        queryset=Country.objects.defer('shape').filter(macro_areas__name = 'EU').order_by('name_en', 'name_iso', 'name_local'),
        help_text=_("The countries you want the data for"),
        widget=forms.SelectMultiple(attrs={'class':'selectpicker'}),
    )
...
...

The attrs of the widgets defined in the fields are no rendered at all.

I monkeypatched this way:

from floppyforms import widgets

def build_attrs(self, base_attrs, extra_attrs=None):
    "Helper function for building an attribute dictionary."
    attrs = base_attrs.copy()
    if extra_attrs is not None:
        attrs.update(extra_attrs)
    else:
        if self.attrs:
            attrs.update(self.attrs)
    return attrs

widgets.Widget.build_attrs = build_attrs

I there any more correct way to do so?

Could I submit a patch?

Thanks.

rtpg commented 4 years ago

@aquilante the 1.7.0 release didn't support Django 1.11, when you have a moment would you be able to try out 1.8.0 and see if your patch is still needed? If not we can look more deeply into this problem

rtpg commented 4 years ago

OK I actually loaded up your snippet in the project and confirmed attributes do render properly when using 1.8.0

        <input type="text" name="date_end" required class="dateinput" id="id_date_end">