Django:我如何创建一个多选择表单? [英] Django: How can i create a multiple select form?
问题描述
例如,我不知道以下哪一个最适合多选择表单。
'ChoiceField','MultipleChoiceField',
'ComboField','MultiValueField',
'TypedChoiceField' TypedMultipleChoiceField'
这里是我需要创建的形式。
< form action =method =postaccept-charset =utf-8>
< select name =countriesid =countriesclass =multiselectmultiple =multiple>
< option value =AUTselected =selected>奥地利< / option>
< option value =DEUselected =selected>德国< / option>
< option value =NLDselected =selected>荷兰< / option>
< option value =USA> United States< / option>
< / select>
< p>< input type =submitvalue =Continue& rarr>< / p>
< / form>
编辑:
另一个小问题。如果我想添加到每个选项一个属性,如数据:
< option value = AUTselected =selecteddata-index = 1> Austria< / option>
我如何做ti?
感谢任何帮助!
我认为CheckboxSelectMultiple应该工作。根据你的问题,在你的forms.py wirite以下代码
从django导入表单
CountryForm(forms.Form):
OPTIONS =(
(AUT,Austria),
(DEU,Germany),
,Neitherlands),
)
Countries = forms.MultipleChoiceField(widget = forms.CheckboxSelectMultiple,
choices = OPTIONS)
编辑:我想到编写完整的代码流,这样你可以更好地理解它。因为您可能会感到困惑
在您的Views.py中定义以下功能
def countries_view(request):
如果request.method =='POST':
form = CountryForm(request.POST)
如果form.is_valid():
countries = form.cleaned_data.get('countries')
#用你的结果做某事
else:
form = CountryForm
return render_to_response('render_country.html ',{'form':form},
context_instance = RequestContext(request))
在您的render_country.html
< form method ='post'>
{%csrf_token%}
{{form.as_p}}
< input type ='submit'value ='submit'>
< / form>
我希望这有帮助。让我知道这是否是你期待的。
I'm beginner in Django/Python and i need to create a multiple select form. I know it's easy but i can't find any example. I know how to create a CharField with a widget but i get confused of all the options inside fields.py.
For example i don't know which one of the followings is best for a multiple select form.
'ChoiceField', 'MultipleChoiceField',
'ComboField', 'MultiValueField',
'TypedChoiceField', 'TypedMultipleChoiceField'
And here is the form i need to create.
<form action="" method="post" accept-charset="utf-8">
<select name="countries" id="countries" class="multiselect" multiple="multiple">
<option value="AUT" selected="selected">Austria</option>
<option value="DEU" selected="selected">Germany</option>
<option value="NLD" selected="selected">Netherlands</option>
<option value="USA">United States</option>
</select>
<p><input type="submit" value="Continue →"></p>
</form>
EDIT:
One more small question. If i want to add to each option one more attribute like data:
<option value="AUT" selected="selected" data-index=1>Austria</option>
How can i do ti?
Thanks for any help!
I think CheckboxSelectMultiple should work. According to your problem, In your forms.py wirite the below code
from django import forms
class CountryForm(forms.Form):
OPTIONS = (
("AUT", "Austria"),
("DEU", "Germany"),
("NLD", "Neitherlands"),
)
Countries = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=OPTIONS)
EDIT : I thought of writing complete code flow So that you can understand it better. Because you might get confuse
In your Views.py define the following function
def countries_view(request):
if request.method == 'POST':
form = CountryForm(request.POST)
if form.is_valid():
countries = form.cleaned_data.get('countries')
# do something with your results
else:
form = CountryForm
return render_to_response('render_country.html', {'form':form },
context_instance=RequestContext(request))
In your render_country.html
<form method='post'>
{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='submit'>
</form>
I hope this helps.Let me know if it that is what you were expecting.
这篇关于Django:我如何创建一个多选择表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!