0

说我有这样的模型:

 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 中,根据用户已经选择的组件名称(可在会话数据中访问)生成带有这些复选框的表单的最佳方法是什么。

非常感谢任何帮助。

4

2 回答 2

0

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

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})

上面的代码未经测试,但我希望它应该可以工作。请让我知道情况如何。

于 2012-11-21T01:07:07.480 回答
0

表单向导正是您所需要的。

https://docs.djangoproject.com/en/1.7/ref/contrib/formtools/form-wizard/

请参阅此处显示的示例https://docs.djangoproject.com/en/1.7/ref/contrib/formtools/form-wizard/#usage-of-namedurlwizardview

以及此处如何定义表单https://docs.djangoproject.com/en/1.7/ref/contrib/formtools/form-wizard/#conditionally-view-skip-specific-steps

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

我的应用程序/forms.py

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',]

我的应用程序/views.py

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],
        })

我的应用程序/urls.py

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'),
)
于 2014-12-06T00:33:59.757 回答