← flask-blog

file upload

c6ca65342487ce35b23bb1510cd26f03e03abf87 · ovidiucs · May 14, 2015 00:28

+62 −8 · 2 files changed

  • flask-blog.py +37 −8
  • templates/upload.html +25 −0
diff --git a/flask-blog.py b/flask-blog.py
index ab022ba..3324263 100644
--- a/flask-blog.py
+++ b/flask-blog.py
@@ -1,8 +1,10 @@
-from flask import Flask, render_template,request
+import os
+import imghdr
+from flask import Flask, render_template, request
 from flask.ext.bootstrap import Bootstrap
 from flask.ext.wtf import Form
-from wtforms import StringField, SubmitField
-from wtforms.validators import Required,Length
+from wtforms import StringField, SubmitField, FileField, ValidationError
+from wtforms.validators import Required, Length
 
 
 app = Flask(__name__)
@@ -11,11 +13,24 @@ app.config['SECRET_KEY'] = 'top secret!'
 # initialize extension
 bootstrap = Bootstrap(app)
 
+
 class NameForm(Form):
     name = StringField('What is your name?', validators=[Required(),
-                                                         Length(1,16)])
+                                                         Length(1, 16)])
+    submit = SubmitField('Submit')
+
+
+class UploadForm(Form):
+    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 imghdr.what(field.data) != 'jpeg':
+            raise ValueError('Invalid image format')
+
+
 @app.route('/')
 def hello_world():
     return render_template('index.html')
@@ -26,7 +41,7 @@ def user(name):
     return render_template('user.html', name=name)
 
 
-@app.route('/jin2/loop')
+@app.route('/loop')
 def index():
     months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
               'August', 'September', 'October', 'November', 'December']
@@ -50,25 +65,39 @@ def index():
     return render_template('weather-average.html', city='Portland, OR',
                            months=months, weather=weather, highlight=highlight)
 
-@app.route('/jin2/form', methods=['GET','POST'])
+
+@app.route('/form', methods=['GET', 'POST'])
 def findex():
     name = None
     if request.method == 'POST' and 'name' in request.form:
         name = request.form['name']
     return render_template('form.html', name=name)
 
-@app.route('/jin2/form2', methods=['GET', 'POST'])
+
+@app.route('/form2', methods=['GET', 'POST'])
 def index3():
     name = None
     form = NameForm()
     if form.validate_on_submit():
         name = form.name.data
         form.name.data = ''
-    return render_template('form2.html', form=form,name=name)
+    return render_template('form2.html', form=form, name=name)
+
+
+@app.route('/upload', methods=['GET', 'POST'])
+def index4():
+    image = None
+    form = UploadForm()
+    if form.validate_on_submit():
+        image = 'uploads/' + form.image_file.data.filename
+        form.image_file.data.save(os.path.join(app.static_folder, image))
+    return render_template('upload.html', form=form, image=image)
+
 
 @app.errorhandler(404)
 def not_found(e):
     return render_template('404.html')
 
+
 if __name__ == '__main__':
     app.run(debug=True)
diff --git a/templates/upload.html b/templates/upload.html
new file mode 100644
index 0000000..a39fa15
--- /dev/null
+++ b/templates/upload.html
@@ -0,0 +1,25 @@
+{% extends "bootstrap/base.html" %}
+{% import "bootsrap/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>
+        </div>
+    </nav>
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        <div class="row">
+            <div class="col-md-4">
+                {{ wtf.quick_form(form, enctype='multiport/form-data') }}
+            </div>
+        </div>
+        {% if image %}
+            <img src="{{ url_for('static', filename=image) }}">
+        {% endif %}
+    </div>
+{% endblock %}
\ No newline at end of file