上传时没有检测到Flask文件 [英] Flask file not detecting on upload

查看:153
本文介绍了上传时没有检测到Flask文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



logo_image = FileField('logo_image',validators = [FileRequired(),FileAllowed( )我的表单看起来像这样:


$ b $

 < form action =method =POSTname =app_brandingenctype =multipart / form-data> 
{{form.csrf_token}}
{{form.brand.label}} {{form.brand}}
{{form.logo_image.label}} {{form.logo_image} }
{{form.title_text.label}} {{form.title_text}}
{{form.first_paragraph.label}} {{form.first_paragraph}}
{{form.faq .label}} {{form.faq}}
{{form.privacy_policy.label}} {{form.privacy_policy}}
{{form.success_message.label}} {{form.success_message} }
{{form.submit.label}} {{form.submit}}
< / form>

对于调试,在我看来,我把:

  @expose('/',methods = ['GET','POST'])
def index(self):
form = BrandForm request.form)
print(form.validate())
print(form.errors)
print(request.files)
print(request.files)

在控制台中,我得到了需要logo_image的消息,即使它在request.files中:

  False 
{'logo_image':['此字段是必填字段。']}
请求。文件
ImmutableMultiDict([('logo_image',< FileStorage:u'20140725_095232.jpg'('image / jpeg')>)])

如何获取FileRequired()方法来检测文件?

解决方案

request.form 仅包含表单输入数据。 request.files 包含文件上传数据。您需要将两者的组合传递给表单。由于你的表单继承自Flask-WTF的 Form (现在叫做 FlaskForm ),所以它会自动处理, ()

  form = BrandForm()

if form.validate_on_submit() :
...

没有Flask-WTF,使用 CombinedMultiDict 来合并数据并将其传递给表单。

  from werkzeug.datastructures import CombinedMultiDict 
$ b $ form = BrandForm(CombinedMultiDict((request.files,request.form)))

if request.method =='POST'and form.validate():
...


I made a flask_wtf Form with this field:

logo_image = FileField('logo_image', validators=[FileRequired(), FileAllowed(['jpg', 'png'], 'Images only!')])

My form looks like this:

<form action="" method="POST" name="app_branding" enctype="multipart/form-data">
    {{ form.csrf_token }}
    {{ form.brand.label }} {{ form.brand }}
    {{ form.logo_image.label }} {{ form.logo_image }}
    {{ form.title_text.label }} {{ form.title_text }}
    {{ form.first_paragraph.label }} {{ form.first_paragraph }}
    {{ form.faq.label }} {{ form.faq }}
    {{ form.privacy_policy.label }} {{ form.privacy_policy }}
    {{ form.success_message.label }} {{ form.success_message }}
    {{ form.submit.label }} {{ form.submit }}
</form>

For debugging, in my view, I put:

@expose('/', methods=['GET', 'POST'])
def index(self):
    form = BrandForm(request.form)
    print(form.validate())
    print(form.errors)
    print("request.files")
    print(request.files)

And in the console I get the message that logo_image is required, even though it is there in request.files:

False
{'logo_image': ['This field is required.']}
request.files
ImmutableMultiDict([('logo_image', <FileStorage: u'20140725_095232.jpg' ('image/jpeg')>)])

How do I get the FileRequired() method to detect the file?

解决方案

request.form only contains form input data. request.files contains file upload data. You need to pass the combination of both to the form. Since your form inherits from Flask-WTF's Form (now called FlaskForm), it will handle this automatically if you don't pass anything to the form.

form = BrandForm()

if form.validate_on_submit():
    ...

Without Flask-WTF, use a CombinedMultiDict to combine the data and pass that to the form.

from werkzeug.datastructures import CombinedMultiDict

form = BrandForm(CombinedMultiDict((request.files, request.form)))

if request.method == 'POST' and form.validate():
    ...

这篇关于上传时没有检测到Flask文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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