python类FileField()的实例源码

test_form.py 文件源码 项目:sanic-wtf 作者: pyx 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
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__)


问题


面经


文章

微信
公众号

扫码关注公众号