Django FormWizards:如何在表单之间无痛地传递用户输入的数据? [英] Django FormWizards: How to painlessly pass user-entered data between forms?

查看:152
本文介绍了Django FormWizards:如何在表单之间无痛地传递用户输入的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django 1.4.3中使用了FormWizard功能。



我已经成功创建了一个四步形式。在表单的前3个步骤中,它正确地从用户那里获取信息,对其进行验证等。在步骤4中,它现在只显示一个确认按钮。没有其他的。当您在步骤4中点击确认时,在done()函数中执行一些有用的功能。到目前为止,它一切正常。



但是,我想使其在第4步(确认步骤)中显示用户输入的数据在以前的审查步骤。我试图找出最无痛的方式来实现这一点。到目前为止,我正在创建一个名为formList的内容,其中包含已经完成的表单列表。

  class my4StepWizard(SessionWizardView):

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

def get_context_data(self,form,** kwargs):
context = super(my4StepWizard,self).get_context_data(form = form,** kwargs)
formList = [self.get_form_list()[i [0]] for my in myWizardForms [:self.steps.step0]]

context.update(
{
'formList':formList,
}

返回上下文


def done(self,form_list,** kwargs)
#在这里做某事。
return HttpResponseRedirect('/ doneWizard')

Form#1有一个名为myField的输入字段。
所以在我的第4步模板中,我想做{{formList.1.clean_myField}}。但是,当我这样做时,我会收到以下错误:



异常值:

'my4StepWizard'对象没有属性'clean_data '



看来,我放入formList的表单是无界的。所以他们不包含用户的数据。有没有可以用来获取数据的修复?我真的很想使用上下文来传递数据,正如我在上面所做的那样。

解决方案

尝试这个:

  def get_context_data(self,form,** kwargs):
previous_data = {}
current_step = self.steps当前#0为第一个形式,1为第二个形式..

如果current_step =='3':#假设没有步骤被跳过,这将是最后一个表单
的计数在范围(3):
previous_data [unicode(count)] = self.get_cleaned_data_for_step(unicode(count))

context = super(my4StepWizard,self).get_context_data(form = form, ** kwargs)
context.update({'previous_cleaned_data':previous_data})
返回上下文

previous_data 是一个字典,其键是向导的步骤(0索引)。每个密钥的项目是步骤中与表单相同的表单的 clean_data


I'm using the FormWizard functionality in Django 1.4.3.

I have successfully created a 4-step form. In the first 3 steps of the form it correctly takes information from the user, validates it, etc. In Step #4, it right now just shows a "Confirm" button. Nothing else. When you hit "Confirm" on step #4, does something useful with it in the done() function. So far it all works fine.

However, I would like to make it so that in step 4 (The confirmation step), it shows the user the data they have entered in the previous steps for their review. I'm trying to figure out the most painless way to make this happen. So far I am creating an entry in the context called formList which contains a list of forms that have already been completed.

class my4StepWizard(SessionWizardView):

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

    def get_context_data(self, form, **kwargs):
        context = super(my4StepWizard, self).get_context_data(form=form, **kwargs)
        formList = [self.get_form_list()[i[0]] for i in myWizardForms[:self.steps.step0]]

        context.update(
            {
                'formList': formList,
            }
        )
        return context        


    def done(self, form_list, **kwargs):
        # Do something here.
        return HttpResponseRedirect('/doneWizard')

Form #1 has an input field called myField. So in my template for step #4, I would like to do {{ formList.1.clean_myField }}. However, when I do that, I get the following error:

Exception Value:
'my4StepWizard' object has no attribute 'cleaned_data'

It seems that the forms I am putting into formList are unbounded. So they don't contain the user's data. Is there a fix I can use to get the data itself? I would really like to use the context to pass the data as I'm doing above.

解决方案

Try this:

def get_context_data(self, form, **kwargs):
    previous_data = {}
    current_step = self.steps.current # 0 for first form, 1 for the second form..

    if current_step == '3': # assuming no step is skipped, this will be the last form
        for count in range(3):
            previous_data[unicode(count)] = self.get_cleaned_data_for_step(unicode(count))

    context = super(my4StepWizard, self).get_context_data(form=form, **kwargs)
    context.update({'previous_cleaned_data':previous_data})
    return context

previous_data is a dictionary and its keys are the steps for the wizard (0 indexed). The item for each key is the cleaned_data for the form in the step which is the same as the key.

这篇关于Django FormWizards:如何在表单之间无痛地传递用户输入的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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