相同的形式,不同的变量。如何实现? [英] same form, different variables. how to implement?

查看:80
本文介绍了相同的形式,不同的变量。如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中一个字段是类别:体育,电影,书籍等

我有另一个表与子类别,例如体育分为芭蕾,futebol,高尔夫,等等,等等。



我有一个包含两个变量的列表:['Sport','Movies']

变量来自一个文件,我正在做一个上传文件,每次该类别都有多个子选项时,我想显示一个带有该类别中的选项的选择。)


$例如,b $ b

,用户插入运动和电影。我想为列表中的每个字段显示一个选择选项。但是子选项不一样,这取决于所选择的类别。



输出:



运动:

o Futebol

o芭蕾

o高尔夫



电影:

o戏剧

o喜剧



forms.py

  class OptionsForm(forms.Form):
def __init __(self, numb,* args,** kwargs):
super(OptionsForm,self).__ init __(* args,** kwargs)
self.fields ['num'] = forms.ChoiceField(widget = RadioSelect (),choices =
numb,label =,required = True)

'通过一个元组与所有子选项:

  numb =(('ballet','ballet'),
('futebol','futebol'),
('golf','golf'),
('drama','drama'),
('comedy' '喜剧'))

但我不知道如何区分它们,也许与formets这将工作,不知道如何



任何帮助或建议会感谢!

谢谢

解决方案

其实比我预期的更容易:)



我只需要传递一个元组的字典:

  dic = {'运动':[('芭蕾','芭蕾'),('futebol','futebol'),('高尔夫'),('高尔夫')],
' ,戏剧),(喜剧,喜剧)]}

并更改表单:

  class CategoriesForm(forms.Form):
def __init __(self,numb,* args,** kwargs) :
super(CtaegoriesForm,self).__ init __(* args,** kwargs)
for p,i in numb.items():
self.fields [p] = forms.ChoiceField (widget = RadioSelect(),choices = i,
required = True)


I have a table where one of the fields is categories: Sports, Movies, Books, etc
I have another table with sub-categories, for instance sports is divided into ballet, futebol, golf, etc, and so on.

Let's say I've a list with two variables : ['Sport', 'Movies']
(These variables came from a file. I'm doing an upload file, and every time the category has more than one sub-option, I want to show a select with the option inside the category.)

for instance, the user inserted sport and movies. I want to show a select option for each one of the field inside the list. But the sub-options are not the same, it depends on the category chosen.

Output:

Sport:
o Futebol
o Ballet
o Golf

Movies:
o Drama
o Comedy

forms.py

class OptionsForm(forms.Form):
def __init__(self, numb, *args, **kwargs):
    super(OptionsForm, self).__init__(*args, **kwargs)
    self.fields['num'] = forms.ChoiceField(widget=RadioSelect(), choices=
        numb, label="", required=True)

I'm passing a tuple with all the sub-options:

numb = (('ballet', 'ballet'),
       ('futebol', 'futebol'),
       ('golf', 'golf'),  
       ('drama', 'drama'),
       ('comedy', 'comedy'))

but I'm not sure how to distinguish them, perhaps with formsets this will work, not sure how although

Any help or suggestion would be appreciate!
Thanks

解决方案

Actually it was more easy than I was expected :)

I just needed to pass a dictionary of tuples:

dic = { 'Sport': [('ballet', 'ballet'), ('futebol', 'futebol'), ('golf'), ('golf')],
        'Movies': [('drama', 'drama'), ('comedy', 'comedy')] }

and change forms:

class CategoriesForm(forms.Form):
    def __init__(self, numb, *args, **kwargs):
        super(CtaegoriesForm, self).__init__(*args, **kwargs)
        for p, i in numb.items():
            self.fields[p] = forms.ChoiceField(widget=RadioSelect(), choices=i,
                required=True)

这篇关于相同的形式,不同的变量。如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆