0

I can't initialize the Choicefield form inside the views.py. I tried passing the option variable in the __init__ function but I got an error:

__init__() takes at least 2 arguments (1 given)` coming from the `form = super(SomeeWizard, self).get_form(step, data, files)

forms.py

class SomeForm(forms.Form):
        def __init__(self, choice, *args, **kwargs):
            super(SomeForm, self).__init__(*args, **kwargs)
            self.fields['choices'] = forms.ChoiceField(choices=[ (o.id, str(o)) for o in choice])

views.py

class SomeWizard(SessionWizardView):

    def get_form(self, step=None, data=None, files=None):
        form = super(SomeWizard, self).get_form(step, data, files)

        if step == "step2":
            option = self.get_cleaned_data_for_step("step1")['choice']

            choice = Choice.objects.filter(question__text__exact=option)

            form = SomeForm(choice)

        return form

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def done(self, form_list, **kargs):
        return render_to_response('done.html')

EDIT

I tried Hasan solution and Django Form Wizard is passing {'files': None, 'prefix': 'step2', 'initial': {}, 'data': None} into the **kwarg in the __init__ function of SomeForm.

I printed the content in the **kwarg and I got:

{'files': None, 'prefix': 'step2', 'initial': {}, 'data': None} {'choices': [<Choice: Blue>, <Choice: Red>]}

4

2 回答 2

0

试试这个改变(我评论改变线):

forms.py

class SomeForm(forms.Form):
    def __init__(self, *args, **kwargs): #this line changed
        choice = kwargs.pop('choice', None) #this line added
        self.fields['choices'] = forms.ChoiceField(choices=[ (o.id, str(o)) for o in choice])
        super(SomeForm, self).__init__(*args, **kwargs)

views.py

class SomeWizard(SessionWizardView):

    def get_form(self, step=None, data=None, files=None):
        form = super(SomeWizard, self).get_form(step, data, files)

        if step == "step2":
            option = self.get_cleaned_data_for_step("step1")['choice']

            choice = Choice.objects.filter(question__text__exact=option)

            form = SomeForm(choice=choice) #this line changed

        return form

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def done(self, form_list, **kargs):
        return render_to_response('done.html')
于 2014-10-13T20:21:10.920 回答
0

仅供参考,我必须使用 data 属性初始化表单才能正常工作。例如:

class SomeWizard(SessionWizardView):

    def get_form(self, step=None, data=None, files=None):
        form = super(SomeWizard, self).get_form(step, data, files)

        # determine the step if not given
        if step is None:
            step = self.steps.current

        if step == "2":
            option = self.get_cleaned_data_for_step("1")['choice']

            choice = Choice.objects.filter(question__text__exact=option)


            ## Pass the data when initing the form, which is the POST
            ## data if the got_form function called during a post
            ## or the self.storage.get_step_data(form_key) if the form wizard
            ## is validating this form again in the render_done methodform 
            form = SomeForm(choice=choice, data=data) 

        return form

def get_template_names(self):
    return [TEMPLATES[self.steps.current]]

def done(self, form_list, **kargs):
    return render_to_response('done.html')

否则,当提交表单时,它只会让我回到第一个表单。有关完整说明,请参见此处。我正在使用 django 1.8。

于 2015-09-24T01:13:27.087 回答