FormWizard和FileFields(Django 1.4) [英] FormWizard and FileFields (Django 1.4)

查看:115
本文介绍了FormWizard和FileFields(Django 1.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的FormWizard(Django 1.4)允许用户来回移动,直到他完成向导。向导保留用户填写的所有值,并显示它们,以防用户回到已经完成的步骤。

My FormWizard (Django 1.4) allows the user to step back and forth until he completes the wizard. The wizard keeps all the values the user filled in and displays them in case the user goes back to a step he already completed.

这样工作正常,即CharField,但不为FileFields工作。如果用户在包含FileField的步骤中提交文件,并且稍后返回到此步骤,则必须再次上传文件。

This works fine i.e. for CharField but does not work for FileFields. In case the user submits a file in a step containing a FileField and later goes back to this step, he has to upload a file again.

有没有一种方式用户不需要重新上传文件?

Is there a way the user won't have to re-upload the file?

请注意,表单数据尚未保存到数据库中。

Please note that the form data have not yet been saved to the database.

推荐答案

我最近遇到了同样的问题,可以通过对Django的SessionWizardView进行子类化(在我的case NamedUrlSessionWizardView中)来解决它,并覆盖get_form方法。

I recently run into the same problem, and could solve it by subclassing Django's SessionWizardView (in my case NamedUrlSessionWizardView), and overriding the get_form method.

基本上我做了以下:


  • 获取已存储的步骤文件。

  • 迭代当前提交的文件。

  • 如果提交的文件为None,请忽略它,否则覆盖已经存储的值。

以下是代码:

from django.contrib.formtools.wizard.views import NamedUrlSessionWizardView

class MyWizardView(NamedUrlSessionWizardView):

    def get_form(self, step=None, data=None, files=None):
        if step:
            step_files = self.storage.get_step_files(step)
        else:
            step_files = self.storage.current_step_files

        if step_files and files:
            for key, value in step_files.items():
                if files.has_key(key) and files[key] is not None:
                    step_files[key] = files[key]
        elif files:
            step_files = files

        return super(MyWizardView, self).get_form(step, data, step_files)

这篇关于FormWizard和FileFields(Django 1.4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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