django-forms-bootstrap icon indicating copy to clipboard operation
django-forms-bootstrap copied to clipboard

Django updated fields in later versions and accessing choice labels and values has changed

Open jdgodchaux opened this issue 8 years ago • 0 comments

Hello! I was having trouble rendering radio buttons using your bootstrap filter, and after some trial and error in field.html, I was able to update that template to handle radio buttons. The following update to this template solves this problem:

{% if field|css_class == "radioselect" %}
    <div class="{{ css_classes.wrap }}">
        {% include "bootstrap/_field_errors_block.html" %}
        {% for choice in field %}
            <div class="radio">
                <label>
                    <input type="radio" {% if choice.choice_value|stringformat:"s" == field.value|stringformat:"s" %}checked="checked" {% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.choice_value }}"> {{ choice.choice_label }}
                </label>
            </div>
        {% endfor %}
        {% include "bootstrap/_field_help_text.html" %}
    </div>
{% endif %}

The main change involved updating the array the for loop loops over from field.field.choices to simply field. From there, one can access the value and label with {{ choice.choice_value }} and {{ choice.choice_label }}. I'm, of course, not sure if this is the best way to update this filter, and I haven't attempted to implement any of the other css_classes in field.html.

jdgodchaux avatar Nov 15 '16 01:11 jdgodchaux