← flask-blog

file upload fix

f3751d0b63543cf89943832d7520fb82f5a58c4e · ovidiucs · May 14, 2015 08:26

+7 −9 · 2 files changed

  • flask-blog.py +4 −6
  • templates/upload.html +3 −3
diff --git a/flask-blog.py b/flask-blog.py
index 3324263..1ef63a6 100644
--- a/flask-blog.py
+++ b/flask-blog.py
@@ -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():
diff --git a/templates/upload.html b/templates/upload.html
index a39fa15..5d15ecd 100644
--- a/templates/upload.html
+++ b/templates/upload.html
@@ -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 %}