How can I add a attrs value to the label in MultipleChoiceFilter?

446 views
Skip to first unread message

Seong Gyu Choi

unread,
Jul 7, 2020, 10:24:11 PM7/7/20
to django-filter
I would like to add class as a property value to the label automatically generated by django-filter.

Below is my code py

[ filter.py ]

...
my_qs
= (
   
('Y', 'Registerd'),
   
('N', 'Not Registerd'),
)

has_registered
= django_filter.MultipleChoiceFilter(choice=my_qs, widget=CheckboxSelectMultiple())


...



[ register.html ]

...


{% bootstrap_field filter.form.has_registered layout='inline' %}
...


[ page source in browser ]

<div class="form-group has-success">
   
<label class="sr-only">Has registered contains</label>
   
<div id="id_has_registered">
       
<div class="checkbox">
           
<label for="id_has_registered_0">
               
<input type="checkbox" name="has_registered" value="Y" class="" title="" id="id_has_registered_0">Registered
           
</label>
       
</div>
       
<div class="checkbox">
           
<label for="id_has_registered_1">
               
<input type="checkbox" name="has_registered" value="N" class="" title="" id="id_has_registered_1">Not Registered
           
</label>
       
</div>
   
</div>
</div>


I would like to add class attributes to this part of the above source for <label for="id_has_registed_0"> and <label for="id_has_registed_1">.

[ page source what i want to change ]

<div class="form-group has-success">
   
<label class="sr-only">Has registered contains</label>
   
<div id="id_has_registered">
       
<div class="checkbox">
           
<!-- Change Here -->
           
<label for="id_has_registered_0" class="btn btn-primary active">
               
<input type="checkbox" name="has_registered" value="Y" class="" title="" id="id_has_registered_0">Registered
           
</label>
       
</div>
       
<div class="checkbox">
           
<!-- Change Here -->
           
<label for="id_has_registered_1" class="btn btn-primary">
               
<input type="checkbox" name="has_registered" value="N" class="" title="" id="id_has_registered_1">Not Registered
           
</label>
       
</div>
   
</div>
</div>




Carlton Gibson

unread,
Jul 8, 2020, 3:04:22 AM7/8/20
to django...@googlegroups.com
Hi.


You can customise the widget used by the underlying form Field, passing attrs. 

--
You received this message because you are subscribed to the Google Groups "django-filter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-filte...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-filter/0912b0a6-dcaf-4041-b5d1-0bb1a2712985o%40googlegroups.com.

Seong Gyu Choi

unread,
Jul 8, 2020, 3:40:03 AM7/8/20
to django-filter
Hi, Carlton

Thanks for replying.

I already used the widget function that you answered.

However, the attrs of the widget are properties of the <input>, but they did not affect the properties of the automatically generated parent <label>.

Also, the above answers do not give a different 'class' attr's value for each query.


[ changed filter.py ]

has_registered = django_filter.MultipleChoiceFilter(choice=my_qs, widget=CheckboxSelectMultiple(attrs={'class': 'btn btn-primary active'}))


If I misunderstood, could you explain even a simple sudo pseud code?

Best Regards,
Choi

Carlton Gibson

unread,
Jul 8, 2020, 4:04:55 AM7/8/20
to django...@googlegroups.com
Sorry, I don’t have time to write the code for you, but if you need per-request adjustments then you can override __init__() to adjust the filters before accessing the form. 

--
You received this message because you are subscribed to the Google Groups "django-filter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-filte...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages