V
V
Vyacheslav Grachunov2019-04-27 00:41:35
Django
Vyacheslav Grachunov, 2019-04-27 00:41:35

How to assign classes to checkboxes using django-widget-tweaks?

forms.CheckboxSelectMultiple generates a list of checkboxes like this:

<ul id="id_items">
    <li><label for="id_items_0"><input type="checkbox" name="items" value="1" checked id="id_items_0">
 Item1</label>

</li>
    <li><label for="id_items_1"><input type="checkbox" name="items" value="2" checked id="id_items_1">
 Item2</label>

</li>
    <li><label for="id_items_2"><input type="checkbox" name="items" value="3" checked id="id_items_2">
 Item3</label>

</li>
    <li><label for="id_items_3"><input type="checkbox" name="items" value="4" checked id="id_items_3">
 Item4</label>

</li>
    <li><label for="id_items_4"><input type="checkbox" name="items" value="5" checked id="id_items_4">
 Item5</label>

</li>
    <li><label for="id_items_5"><input type="checkbox" name="items" value="6" checked id="id_items_5">
 Item6</label>

</li>
</ul>

Through django-widget-tweaks, it turns out to assign a class only for ul, because it's actually a widget.
{% for field in formEx %}
    {% render_field field|append_attr:"class:test" %}
{% endfor %}

And after that, the test class is assigned to the ul element.
How can I assign classes to input without touching the ul?
Actually, you need to arrange everything in bootstrap4.
For the second day I can not figure it out

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Grachunov, 2019-04-27
@Qwentor

It turned out with the help of django-crispy-forms. Just this entry and that's it:
{{ formEx|crispy }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question