Django forloop和所有可选单选按钮

时间:2019-04-30 14:04:38

标签: python django django-forms bootstrap-4

这是一个看似简单的任务,它使用一组记录创建表单,以便用户可以选择要使用的记录,所有这些都使用单选按钮。

<form action="" method="GET">{% csrf_token %}
{% for record in select_records %}
    <div class="form-check indent-3"> 

        <label class="form-check-label" for="radio{{forloop.counter}}">
            <input type="radio" class="form-check-input" id="radio{{forloop.counter}}" name="{{record.get_model_name}}{{record.id}}" value="{{record.record_name}}">

            {% if request.user.userprofile.head_shot_thumb %}

                <img src="{{ request.user.userprofile.head_shot_thumb }}" alt="Proforma creator"> 

            {% else %}

                <div class="h2-li ">
                    <i class="fas fa-user"></i> 
                </div>
            {% endif %}

            {{ record.record_name }} - {{ record.date_created }}
        </label>
    </div>
{% endfor %} 

问题在于表单创建了一个单选按钮列表,这些单选按钮就像所有复选框的可选方式一样。

我已经搜索了我的代码并将其与简单的无线电形式(例如the one at W3schools)进行比较,但我无法弄清楚。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我对您的代码做了一些小的更改。在下面检查。

<form action="" method="GET">
{% csrf_token %}
{% for record in select_records %}
    <div class="form-check indent-3"> 
        <label class="form-check-label" for="radio{{forloop.counter}}">
            <input type="radio" class="form-check-input" id="radio{{forloop.counter}}" name="record-option" value="{{record.record_name}}">
            {% if request.user.userprofile.head_shot_thumb %}
                <img src="{{ request.user.userprofile.head_shot_thumb }}" alt="Proforma creator"> 
            {% else %}
                <div class="h2-li ">
                    <i class="fas fa-user"></i> 
                </div>
            {% endif %}
            {{ record.record_name }} - {{ record.date_created }}
        </label>
    </div>
{% endfor %}
</form>

我希望这会对您有所帮助。 :)