Django:我怎样才能写一个表单.SelectMultiple?

时间:2016-08-10 12:07:59

标签: python html django

我正在为我的问题寻找一个例子,遗憾的是我无法找到合适的问题。

我知道我必须编辑form.py,view.py和我的html文件,但我不确定如何。 我的html文件如下所示:

<div class="form-group">
     <label class="col-sm-2 control-label">Countries</label>
     <div class="col-sm-10">
       <div class="row">
         <div class="col-md-4">
           <select name="country" class="country_selection form-control" multiple="multiple" 
           required oninvalid="this.setCustomValidity('Please choose a country')" oninput="setCustomValidity('')" >

           {% for country in form.country %}
             <option value=" {{ country.id }}">{{country}} </option>
           {% endfor %}
         </select>
       </div>
     </div>
   </div>
 </div>

当我使用

时,它完全可行
<option value="Austria">Austria</option>
<option...

但是,我希望它看起来更像django,所以之后编辑会更容易。

这是我的forms.py:

class NameListForm(forms.Form):

    AUSTRIA = 'at'
    CROATIA = 'cr'
    ENGLAND = 'en'
    GERMANY = 'de'
    FRANCE = 'fr'

    COUNTRY_CHOICES = (  
        (AUSTRIA, 'Austria'),
        (CROATIA, 'Croatia'),
        (ENGLAND, 'England'),
        (GERMANY, 'Germany'),
        (FRANCE, 'France'),
    )

    list_length  = forms.IntegerField(label='Length', min_value=1)
    duplicates   = forms.IntegerField(label='Duplicates', min_value=1)
    country      = forms.MultipleChoiceField(label='Countries',widget=forms.SelectMultiple, choices=COUNTRY_CHOICES)

views.py:

def index(request):
    form = NameList.objects.all()
    #

    if request.method == "GET":
        return render_to_response('name_generator.html', {"form":NameListForm()},
                                  context_instance = RequestContext(request))

    else:
        form = NameListForm(request.POST)
        if form.is_valid():
            list_length = form.cleaned_data["list_length"]
            duplicates  = form.cleaned_data["duplicates"]
            country   = form.cleaned_data["country"]
            if duplicates > (2 * list_length):
                raise forms.ValidationError("Please make sure that the number of duplicates is less then the actual list length.")

            models.NameList.objects.create(list_length=list_length, duplicates=duplicates, databases=country)

            return HttpResponseRedirect('/n-velope/apate/')

        return render_to_response('name_generator.html', { 'form':form },  
                                  context_instance = RequestContext(request))


    return render_to_response('name_generator.html', { "form":form }, 
                              context_instance = RequestContext(request))

有谁知道我是什么,以便它能再次正常工作?

EDIT 我也找到了解决问题的合适解决方案:

forms.py

中的更改
country      = forms.MultipleChoiceField(label='Countries',widget=forms.SelectMultiple(attrs={'class': 'country_selection form-control'}) , choices=COUNTRY_CHOICES)

和模板:

       <div class="col-md-4">
         {{ form.country }
         {{ form.country.errors }}
       </div>

1 个答案:

答案 0 :(得分:0)

我认为问题在于你是在迭代表单小部件而不是它的选择。所以试试这个:

{% for id, country in form.country.choices %}
     <option value="{{ id }}">{{country}}</option>
{% endfor %}