1

I trying to render fields by looping form class field using django-widget-tweaks render_field. Fields are rendered as excepted, however, unable to select an option for combo box. Any Idea to make an option selected by value given.

I have used value attribute to set the values of rendered fields which is working for charfield, but not for combo box.

{% for field in form.visible_fields %}
    <div class="pan-sty">
        <label class="th-color control-label font-normal">{{ attribute.1 }}</label>
        {% render_field field class+="form-control" value|option=records|get_attrs:field.name %}
    </div>
{% endfor %}

Excepted output the value "Indirect-retail" should be selected in combo box

<div class="pan-sty">
    <label class="th-color control-label font-normal">Sales Channel</label>
    <select name="sales_channel" class="form-control" id="id_sales_channel">
        <option value="" selected="">---------</option>

        <option value="Direct-web">Direct - Web</option>

        <option value="Direct-in-store">Direct - In-store</option>

        <option value="Direct-sales-rep">Direct- Sales Representative</option>

        <option value="Indirect-wholesale">Indirect - Wholesale</option>

        <option value="Indirect-retail" selected>Indirect - Retail</option>

        <option value="Indirect-influencer">Indirect - Influencer</option>

        <option value="Indirect-affiliate">Indirect - Affiliate</option>

    </select>
</div>

but value getting added as "value" attribute in select tag

<div class="pan-sty">
    <label class="th-color control-label font-normal">Sales Channel</label>
    <select name="sales_channel" class="form-control" value="Indirect-retail" id="id_sales_channel">
        <option value="" selected="">---------</option>

        <option value="Direct-web">Direct - Web</option>

        <option value="Direct-in-store">Direct - In-store</option>

        <option value="Direct-sales-rep">Direct- Sales Representative</option>

        <option value="Indirect-wholesale">Indirect - Wholesale</option>

        <option value="Indirect-retail">Indirect - Retail</option>

        <option value="Indirect-influencer">Indirect - Influencer</option>

        <option value="Indirect-affiliate">Indirect - Affiliate</option>

    </select>
</div>
Dhaaz
  • 27
  • 4
  • 1
    have you set an initial value on the form? or initialised it with an instance? – dirkgroten Apr 03 '19 at 11:11
  • Hi, thank you, sorry I'm new to django, I didn't initialise the value on the form. It's working great! Thanks again – Dhaaz Apr 04 '19 at 05:50

0 Answers0