Django会话向导将提交上一步

时间:2017-02-01 19:27:59

标签: python django django-forms django-views

我正在尝试使用django SessionWizard视图来完成具有两个步骤的表单。第一步,我获取文件位置,然后在第二步处理文件内容,并在提交第二步时将它们写入db。当我点击提交时,表单将返回第一步。我使用CookieWizardView测试它,表单工作正常 - 我能够在提交第二步时将数据写入db。 对于SessionWizardView,我验证了根据文档here在我的应用程序中启用了会话 - 还验证了会话表在我的数据库中并且是非空的。

已经看过相关questions,但没有多大帮助。

以下是我的观点的代码段

    class UploadWizard(SessionWizardView):

    def __init__(self, *args, **kwargs):
        super(UploadWizard, self).__init__(*args, **kwargs)        
        self.uploaded_file = None
        self.ingestable_upload = None
        self.error = None

    form_list = UPLOAD_FORMS
    # TODO: sweep for old files.  Maybe move to S3?
    upload_dir = os.path.join(settings.MEDIA_ROOT, 'uploads')
    file_storage = FileSystemStorage(location=upload_dir)

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

    def get_form(self, step=None, data=None, files=None):
        if step is None:
            step = self.steps.current
        form = super(UploadBoxofficeWizard, self).get_form(step, data, files)
        if step == MAP_DATA_STEP:
           # do something
        return form

def process_step(self, form):

        if self.steps.current == PICK_FILE_STEP:
            action = form.cleaned_data.get('action')
            extractor = get_upload_extractor_for_source(form.cleaned_data.get('source'))
            uploaded_file = form.cleaned_data.get('file')
            start_date = form.cleaned_data.get('start_date')
            end_date = form.cleaned_data.get('end_date')
            if action == INGEST_ACTION:
                #do something               
        elif self.steps.current == MAP_DATA_STEP:
            # do something

def done(self, form_list, form_dict, **kwargs):       
        form = form_list[int(MAP_DATA_STEP)]
        num_rows= 0
        if form:
            num_rows = form.cleaned_data[0].get('num_rows')
        return render_to_response('formtools/wizard/show_result.html',{'num_rows':num_rows})

会话设置

INSTALLED_APPS = (
    'django.contrib.sessions',
)

MIDDLEWARE_CLASSES = [    
    'django.contrib.sessions.middleware.SessionMiddleware',    
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.RemoteUserMiddleware',    
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

寻求一些帮助,以便了解这里可能出现的问题。我已按照文档中的步骤进行操作,但显然我遗漏了一些内容。

0 个答案:

没有答案