【问题标题】:Django - how to use MultiSelectField in templateDjango - 如何在模板中使用 MultiSelectField
【发布时间】:2020-10-24 18:10:55
【问题描述】:

我正在尝试在 django 模板中添加 MultiSelectField。我实现了以下代码,但是模板中存在问题。但是,在 django admin 中它运行良好。

#models.py

from multiselectfield import MultiSelectField

    class Amenities(models.Model):
        Amenities_Choices =(
            ('Free Wifi','Free Wifi'),
            ('Attach Bathroom', 'Attach Bathroom'),
            ('Shared Bathroom', 'Shared Bathroom'),
            ('Free Laundry Service', 'Free Laundry Service',),
            ('24/7 water facility', '24/7 water facility'),
            ('Geyser', 'Geyser'),
            ('Private TV', 'Private TV'),
        )
        hostel = models.ForeignKey(AddHostel, on_delete=models.CASCADE)
        feature = MultiSelectField(choices=Amenities_Choices)
    
        def __str__(self):
            return self.hostel.location

views.py:

def amenities(request):
    if request.method == "POST":
        hostel = request.POST['hostel']
        feature = request.POST['feature']
        amen = Amenities(hostel_id=hostel, feature=feature)
        amen.save()
    hostels = AddHostel.objects.all()
    return render(request,'staff/amenities.html',{'hostels':hostels})

    <div class="col-lg-12">
                    <form method="post">
                        {% csrf_token %}
                        <div class="row">
                            <div class="form-group col-md-6 col-sm-12">
                                <label for="hostel">Hostel:</label>
                                <select class="form-control form-search-ch" id="hostel" name="hostel">
                                    {% for h in hostels %}
                                        <option value="{{ h.id }}">{{ h.hostel_name }}</option>
                                    {% endfor %}

                                </select>
                                {#                            <input type="text" class="form-control form-grp-lbl" id="hostel" name="hostel"#}
                                {#                                   required>#}
                            </div>
                            <div class="form-group col-md-6 col-sm-12">
<!--in this section checkbox is not showing-->
                                {% for value, text in form.amenities.field.choices %}
                                    <div class="ui slider checkbox">
                                        <input id="{{ forloop.counter0 }}" name="{{ form.feature }}"
                                               type="checkbox" value="{{ feature }}"{% if value in checked_feature %}
                                               checked="checked"{% endif %}>
                                        <label>{{ text }}</label>
                                    </div>
                                {% endfor %}
                            </div>
                            <div class="form-group col-md-12 mx-auto">
                                <input type="submit" class="form-control"
                                       name="register" value="Add"
                                       style="width:150px; height: 50px; font-size: 1.2rem; border-radius: 10px 10px;background: #E35452;  border: 1px solid rgba(0,0,0,0.5); color: #ffffff">
                            </div>
                        </div>
                    </form>
                </div>

【问题讨论】:

    标签: python django django-views django-templates django-multiselectfield


    【解决方案1】:

    您可以轻松使用 .tag 和 .choice_label

    <div class="" style="">
                    <p class="col-sm-8"><b>{{form.feature.label}} :</b></p>
                </div>
                <div>
                    {% for checkbox in form.diet_goal%}
                    <div class="" style="">
                        <span>
                                {{ checkbox.tag }}
                                <label class="" for="{{ checkbox.id_for_label }}">{{ checkbox.choice_label }}</label>
                        </span>
    
                    </div>
                    {% endfor %}
                </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 2011-09-17
      • 2019-02-09
      • 2014-02-17
      • 2011-10-01
      • 2020-09-09
      • 2010-09-23
      相关资源
      最近更新 更多