【发布时间】:2012-03-15 22:12:25
【问题描述】:
我是 Django、GAE 的新手,在尝试使用 Django 构建一些动态表单时遇到了一些问题。这个想法是从一个下拉框中进行的选择决定了另一个字段中的值。
详细来说,如果一个用户
- 在“Species”中选择“A”,则“body_weight”中的值为178。同理,
- 如果他在“物种”中选择“B”,“体重”中的值为1580,并且
- 如果在“物种”中选择“C”,他可以填写“body_weight”
我阅读了一些示例,发现了一些功能,例如“应该包含 init 和 super”。因此我修改了我的代码,但它仍然没有工作。
我的问题是:
- 放置“init”和“super”的正确位置是什么?
- 这里需要“self.field”吗?
欢迎所有建议!
谢谢!
这里是代码。
Species_CHOICES=(('178','A'),('1580','B'),('','C'))
class Inp(forms.Form):
Species = forms.ChoiceField(label='Species', choices=Species_CHOICES, initial='A')
#Here detect the choice made by the user
def get_STB_choices(Species):
if Species=='178':
r= 178
elif Species=='1580':
r= 1580
else:
r= 4440
return a
#assign values to a new parameter based on user's choice
def __init__(self, *args, **kwargs):
super(Inp, self).__init__(*args, **kwargs)
body_weight=forms.FloatField(required=True, label='Body weight', initial=get_STB_choices(Species))
#write out in HTML
class InputPage(webapp.RequestHandler):
def get(self):
html = str(Inp())
self.response.out.write(html)
【问题讨论】:
标签: django google-app-engine django-forms