Python wtforms 模块,FileField() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用wtforms.FileField()

项目:sanic-wtf    作者:pyx    | 项目源码 | 文件源码
def test_file_upload(app):
    app.config['WTF_CSRF_ENABLED'] = False

    class TestForm(SanicForm):
        upload = FileField('upload file')
        submit = SubmitField('Upload')

    @app.route('/upload', methods=['GET', 'POST'])
    async def upload(request):
        form = TestForm(request)
        if form.validate_on_submit():
            return response.text(form.upload.data.name)
        content = render_form(form)
        return response.html(content)

    req, resp = app.test_client.post(
        '/upload', data={'upload': open(__file__, 'rb')})
    assert resp.status == 200
    assert resp.text == os.path.basename(__file__)