Skip to content

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

@jdgodchaux

Description

@jdgodchaux

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions