我有一个很大的django表单。它有很多依赖于所选字段的数据。
例如:
你有车吗?是/否下拉框。
如果是,我想显示其他字段make/model/year/cc等
如果不是,我想把它们隐藏起来。
最好的方法是什么?一个使用jquery的简单的显示/隐藏div,或者有什么我应该做的更好的事情?
发布于 2011-07-12 18:02:38
是的,你应该在客户端使用一个简单的显示/隐藏JQuery。然后在服务器端,如果您还想让某些字段仅在选择了其他字段时才需要,则需要在表单的clean方法中进行条件验证。
例如:
class MyForm(Form):
...
def clean(self):
cleaned_data = self.cleaned_data
has_car_required_fields = ['foo', 'bar', 'baz']
if cleaned_data.has_key('has_car') and cleaned_data['has_car']:
for field_name in has_car_required_fields:
if not cleaned_data.has_key(field_name) or not cleaned_data[field_name]:
self._errors[field_name] = ErrorList([_(u'This field is required')])
return cleaned_datahttps://stackoverflow.com/questions/6661968
复制相似问题