漏勺变形文件上传小部件

Colander deform file upload widget

我想上传具有以下漏勺架构的单个文件:

import colander
import deform
from deform.interfaces import FileUploadTempStore 

@view_config(route_name='upload',
             renderer='templates/upload.pt',
             permission='view')
def upload(self):
    tmpstore = FileUploadTempStore()
    class Schema(colander.Schema):
        name = colander.SchemaNode(
            deform.FileData(),
            widget=deform.widget.FileUploadWidget(tmpstore),
            title='Upload'
        )
    def on_validated(request, captured):
        pass
    schema = Schema()
    form = deform.Form(schema, buttons=('upload',), formid='form')
    html = []
    captured = None
    if 'upload' in self.request.POST:
        try:
            controls = self.request.POST.items()
            captured = form.validate(controls)
            response = on_validated(self.request, captured)
            if response is not None:
                return response
        except deform.ValidationFailure as e:
            html.append(e.render())
    else:
        html.append(form.render())
    html = ''.join(html)
    return {
        'form': html,
    }

视图工作正常,我可以浏览 select 文件。但是,当我按下 "upload" 按钮时,出现错误: TypeError: 'NoneType' object does not support item assignment

定义了controls,但是form.validate()是问题所在。它指的是 deform/widget.py 第 1674 行:

self.tmpstore[uid]['preview_url'] = preview_url

通过不同方式定义 tmpstore(与 deformdemo/init.py 不同):

    class MemoryTmpStore(dict):
        """ Instances of this class implement the
        :class:`deform.interfaces.FileUploadTempStore` interface"""

        def preview_url(self, uid):
            return None

    tmpstore = MemoryTmpStore()