0

我正在尝试使用 flask_uploads 上传图像。验证时,它总是显示需要一个图像(它得到 None 而不是 FileStorage 实例)。我还尝试了不同的图像类型,所以这不是由于文件。

有没有遗漏?非常感谢。

初始化

app = Flask(__name__)
app.config.from_object(config[config_name])
app.config['SECRET_KEY'] = 'development'
app.config['UPLOADED_DEF_DEST'] = './static'
app.config['UPLOADED_DEF_URL'] = './static'

abc = UploadSet(name='DEF', extensions=IMAGES)
configure_uploads(app, abc)

视图.py

@main.route('/edit-profile', methods=['GET', 'POST'])
@login_required
def edit_profile():
    form = EditProfileForm()
    if form.validate_on_submit():
        file_name = abc.save(form.btn_uploads.data)
        file_url = abc.url(file_name)
    return render_template('edit_profile.html', form=form)

表格.py

class EditProfileForm(FlaskForm):
    btn_uploads = FileField(label='Profile Image', validators=[
        FileAllowed(abc, 'ONLY IMAGE ALLOWED'),
        FileRequired('IMAGE REQUIRED PLEASE')
    ])
    submit = SubmitField('Submit')

edit_profile.html

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}Edit Profile{% endblock %}

{% block page_content %}
<div class="page-header">
    <h1>Edit Your Profile</h1>
</div>
<div class="col-md-4">
    {{ wtf.quick_form(form) }}
</div>
{% endblock %}
4

0 回答 0