file upload fix
+7 −9 · 2 files changed
-
flask-blog.py+4 −6 -
templates/upload.html+3 −3
@@ -19,17 +19,15 @@ class NameForm(Form): Length(1, 16)]) submit = SubmitField('Submit') - class UploadForm(Form): - image_file = FileField('Image File') + image_file = FileField('Image file') submit = SubmitField('Submit') def validate_image_file(self, field): - if field.data.filename[-4:].lower() != '.jpg': - raise ValueError('Invalid File Extension') + if field.data.filename.rsplit('.', 1)[1] != 'jpg': + raise ValidationError('Invalid file extension') if imghdr.what(field.data) != 'jpeg': - raise ValueError('Invalid image format') - + raise ValidationError('Invalid image format') @app.route('/') def hello_world(): @@ -1,12 +1,12 @@ {% extends "bootstrap/base.html" %} -{% import "bootsrap/wtf.html" as wtf %} +{% import "bootstrap/wtf.html" as wtf %} {% block title %}File Upload Example{% endblock %} {% block navbar %} <nav class="navbar navbar-inverse" role="navigation"> <div class="container"> - <a class="navbar-brand" href="{{ url_for("index") }}">Upload</a> + <a class="navbar-brand" href="{{ url_for('index') }}">Upload</a> </div> </nav> {% endblock %} @@ -15,7 +15,7 @@ <div class="container"> <div class="row"> <div class="col-md-4"> - {{ wtf.quick_form(form, enctype='multiport/form-data') }} + {{ wtf.quick_form(form, enctype='multipart/form-data') }} </div> </div> {% if image %}