django-crispy-forms / crispy-tailwind

A Tailwind template pack for django-crispy-forms
MIT License
329 stars 56 forks source link

"selected"-Option is missing @SelectMultiple #119

Closed datenrebell closed 4 months ago

datenrebell commented 2 years ago

My Model:

class Service(models.Model):
    staff = models.ManyToManyField(
            User,
            blank=True,
    )

My Form:

class ServiceUpdateForm(forms.ModelForm):
    class Meta:
        model=Service
        exclude = []
        widgets = { 
                'staff': forms.SelectMultiple(
                    attrs={'multiple': 'multiple'}
                ),  
        }

In my template {{form}} generate this:

<select name="staff" multiple="" id="id_staff">
  <option value="1" selected="">name1</option>
  <option value="2" selected="">name2</option>
  <option value="3">name3</option>
</select>

With {{form|crispy}} django generate this:

<select class="bg-white focus:outline-none border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal text-gray-700" name="staff" multiple="multiple"> 
  <option value="1" multiple="multiple">name1</option> 
  <option value="2" multiple="multiple">name2</option> 
  <option value="3" multiple="multiple">name3</option> 
</select>
ghost commented 2 years ago

{{ field.field.widget.attrs|flatatt }} is there in select.html and select_option.html.

How would <option> attributes be properly specified? According to the Mozilla documentation there are attributes like: disabled, label, selected and value. value and selected are already taken care of, but disabled and label not. So I think just removing the {{ field.field.widget.attrs|flatatt }} from select_option.html might not be the best option.

datenrebell commented 2 years ago

@gldecurtins This works, Thanks!

GitRon commented 6 months ago

@gldecurtins I just had a look at https://github.com/django-crispy-forms/crispy-tailwind/pull/120 and it seems to work. Found this issue afterwards... What would be the best fix for the package?