django动态表单生成

django动态表单生成,django,django-forms,Django,Django Forms,假设我有这样一个模型: class ComponentLength(models.Model): component_name = models.CharField(max_length=155) length1 = models.IntegerField() length2 = models.IntegerField() length3 = models.IntegerField() length4 = models.IntegerField() 现

假设我有这样一个模型:

 class ComponentLength(models.Model):
    component_name = models.CharField(max_length=155)
    length1 = models.IntegerField()
    length2 = models.IntegerField()
    length3 = models.IntegerField()
    length4 = models.IntegerField()
现在,我有一个表单供用户选择一个组件,在下一页中,我想为不同的长度选项显示4个复选框,这对于不同的组件是不同的

在Django中,根据用户已经选择的组件名称(在会话数据中可访问)生成带有这些复选框的表单的最佳方式是什么


非常感谢您的帮助。

您可以使用普通的django表单,并在实例化时在init方法中更改其字段。大概是这样的:

class SecondForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(SecondForm, self).__init__(*args, **kwargs)
        try:
            #get the object, something like this:
            obj_ = ComponentLength.objects.get(component_name = session.get('component_name_or_whatever_you_stored'))
        except:
            #handle the error case, e.g:
            return

            self.fields['length1'] = forms.CheckboxInput(attrs={'value' : obj_.length1 })
            self.fields['length2'] = forms.CheckboxInput(attrs={'value' : obj_.length2 })
            self.fields['length3'] = forms.CheckboxInput(attrs={'value' : obj_.length3 })
            self.fields['length4'] = forms.CheckboxInput(attrs={'value' : obj_.length4 })
            #Consider using a hidden input instead of polluting the session variables
            #with form data
            self.fields['component_length'] = forms.HiddenInput(attrs={'value' : obj_.pk})

上面的代码没有经过测试,但我希望它能正常工作。请告诉我进展如何。

表单向导正是您所需要的

请参见此处显示的示例

这里的形式是如何定义的

我没有测试下面的代码,但它应该类似于以下内容:

myapp/forms.py myapp/views.py myapp/url.py
从django.conf.url导入url、模式
从myapp.forms导入ComponentLengthNameForm,ComponentLengthChoicesForm
从myapp.views导入联系人向导
命名联系人表格=(
('name',ComponentLengthNameForm),
(“长度选择”、组件长度选择表),
)
组件向导=组件向导。作为视图(命名为联系人表单,
url\u name='component-wizard-form-step',完成\u step\u name='finished')
urlpatterns=模式(“”,
url(r“^my form/(?P.+)/$”,组件向导,名称='component-wizard-form-step'),
url(r“^my form/$”,组件向导,名称='component-wizard-form'),
)
from django.forms import ModelForm

from myapp.models import ComponentLength

class ComponentLengthNameForm(ModelForm):

    class Meta:
        model = ComponentLength
        fields = ['component_name',]

class ComponentLengthChoicesForm(ModelForm):

    class Meta:
        model = ComponentLength
        fields = ['length1', 'length2', 'length3', 'length4',]
from django.contrib.formtools.wizard.views import SessionWizardView
from django.shortcuts import render_to_response

class ComponentWizard(SessionWizardView):

    def done(self, form_list, **kwargs):
        return render_to_response('done.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })
from django.conf.urls import url, patterns

from myapp.forms import ComponentLengthNameForm, ComponentLengthChoicesForm
from myapp.views import ContactWizard

named_contact_forms = (
    ('name', ComponentLengthNameForm),
    ('length-choices', ComponentLengthChoicesForm),
)

component_wizard = ComponentWizard.as_view(named_contact_forms,
    url_name='component-wizard-form-step', done_step_name='finished')

urlpatterns = patterns('',
    url(r'^my-form/(?P<step>.+)/$', component_wizard, name='component-wizard-form-step'),
    url(r'^my-form/$', component_wizard, name='component-wizard-form'),
)