漏勺变形文件上传小部件

时间:2015-11-28 14:02:56

标签: colander

我想使用以下漏勺架构上传单个文件:

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,
    }

视图工作正常,我可以浏览并选择一个文件。但是,当我按下"上传"按钮,我收到错误: TypeError: 'NoneType' object does not support item assignment

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

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

1 个答案:

答案 0 :(得分:1)

通过不同地定义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()