简短的回答是,这需要CSS,JavaScript和Django的混合形式。
表格
第1步是创建一个形式与条件验证我干净的方法。
from django import forms
from django.utils.translation import ugettext as _
from django.forms.widgets import RadioSelect, Textarea, CheckboxSelectMultiple
from django.utils.safestring import mark_safe
class FormContact(forms.Form):
"""
The contact form
"""
choice_a = forms.ChoiceField(
label=_(u' '),
choices=(
(1, mark_safe(_(u'First Option'))),
(0, mark_safe(_(u'Second Option'))),
),
widget=RadioSelect,
initial=1
)
show_if_choice_1 = forms.CharField(
label=_(u'Choice 1 text box')
)
show_if_choice_2 = forms.CharField(
label=_(u'Choice 2 text box')
)
def clean(self):
super(forms.Form, self).clean()
if 'choice_a' in self.cleaned_data :
if self.cleaned_data['choice_a'] == '1':
if self.cleaned_data['show_if_choice_1'] == '' :
self._errors['show_if_choice_1'] = self.error_class([_(u'Please Fill out choice 1 text box.'),])
if self.cleaned_data['choice_a'] == '2':
if self.cleaned_data['show_if_choice_2'] == '' :
self._errors['show_if_choice_2'] = self.error_class([_(u'Please fill out choice 2 box'),])
return self.cleaned_data
的JS和CSS
写一些JavaScript + CSS的基础上,有条件的字段的值,显示隐藏字段。
注意:如果您需要更多的指导方向,请告诉我,如果您迅速找到jist,我不想花费永远打字。
谢谢,这似乎有帮助;我会在运行一些测试后确认你的答案:) – 2012-02-01 08:43:25