使用flask_uploads

时间:2018-08-20 14:21:15

标签: flask flask-wtforms wtforms flask-uploads

我正在尝试使用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)

views.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)

forms.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 %}

0 个答案:

没有答案