Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ChoiceField #36

Open
filipeximenes opened this issue May 26, 2016 · 10 comments
Open

Support for ChoiceField #36

filipeximenes opened this issue May 26, 2016 · 10 comments

Comments

@filipeximenes
Copy link

It's possible to render choice fields this way:

{% for radio in form.my_radio_field %}
                  {{ radio.tag }}
                  {{ radio.choice_label }}
{% endfor %}

This gives more control over the generated HTML. It's not currently possible to use widget tweaks template tags in this case. Eg.:

{% for radio in form.my_radio_field %}
                  {{ radio.tag|add_class:"some-class" }}
                  {{ radio.choice_label }}
{% endfor %}
@sevetseh28
Copy link

I second this! Im using MultipleChoiceField and would like to customize each item.

@nurzhannogerbek
Copy link

nurzhannogerbek commented Apr 24, 2017

@sevetseh28 Hello! I also use MultipleChoiceField in my form. How you fixed your problem with customization in template?

@autoferrit
Copy link

I have this issues as well. Was choice fields ever given support? For now I am just detecting the field type and handling it appropriately. It is kind of ugly but works.

{% load widget_tweaks %}

{% for hidden_field in form.hidden_fields %}
  {{ hidden_field }}
{% endfor %}

{% if form.non_field_errors %}
  <div class="alert alert-danger" role="alert">
    {% for error in form.non_field_errors %}
      {{ error }}
    {% endfor %}
  </div>
{% endif %}

{% for field in form.visible_fields %}

    {% if form.is_bound %}
      {% if field.errors %}

        {% if field|field_type == 'choicefield' %}
          {% render_field field class="form-control is-invalid custom-select my-2" %}
        {% else %}
          {% render_field field class="form-control is-invalid" placeholder=field.help_text %}
        {% endif %}
        {% for error in field.errors %}
          <div class="invalid-feedback">
            {{ error }}
          </div>
        {% endfor %}
      {% else %}
        {% if field|field_type == 'choicefield' %}
          {% render_field field class="form-control is-valid custom-select my-2" %}
        {% else %}
          {% render_field field class="form-control is-valid" %}
        {% endif %}
      {% endif %}
    {% else %}
      {% if field|field_type == 'choicefield' %}
        {% render_field field class="custom-select my-2" %}
      {% else %}
        {% render_field field placeholder=field.help_text %}
      {% endif %}
    {% endif %}
{% endfor %}

@masystems
Copy link

masystems commented Mar 29, 2020

I did it like this, seems to work fine:

 <div class="form-group col-md-6">
    <label>Species</label>
    <select id="species{{ class.id }}" class="form-control">
        {% for option in class_form.species %}
            {% if option.choice_label == class.species|title %}
                <option value="{{ option.choice_label|lower }}" selected>{{ option.choice_label }}</option>
            {% else %}
                {{ option }}
            {% endif %}
        {% endfor %}
    </select>
</div>

@AntonOfTheWoods
Copy link

Can anyone confirm that this sort of thing is still required for select fields?

@johncronan
Copy link

There's no workaround for this that I can find. @autoferrit's response is not accessing the subwidgets, and @masystems' response is using a select menu. We're talking about fields with a widget that has more than one subwidget, like RadioSelect or CheckboxSelectMultiple.

I think there's a fairly minimal fix. When widget_tweaks sees a BoundWidget instead of a BoundField, the method to wrap would be tag(), rather than as_widget(). @jazzband, will you accept a PR for this?

@johncronan
Copy link

See the attached for an implementation. I guess I'll put in a PR, at some point.
patched_widget_tweaks.py.txt

@realnot
Copy link

realnot commented Mar 7, 2023

Ping! I have the same problem. Any update?

@alfonsrv
Copy link

Still relevant – PR has been available for a while now: #122

@peterstavrou
Copy link

I'm also trying to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests