Created
May 17, 2014 13:14
-
-
Save danielthiel/8f2a1679e48986599b63 to your computer and use it in GitHub Desktop.
flask file upload with Flask-WTF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! python | |
| # -*- coding: utf-8 -*- | |
| from wtforms import fields, validators | |
| from flask.ext.wtf import Form | |
| from flask.ext.wtf.file import FileField, FileAllowed | |
| class UploadForm(Form): | |
| name = fields.TextField(u'Just some banana for scale', [validators.Required()]) | |
| img = FileField(u'Image', validators=[ | |
| FileAllowed(['jpg', 'png'], 'Images only!') | |
| ]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html><html lang="en"> | |
| <head> | |
| <!-- metadata --> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
| </head> | |
| <body> | |
| <form role="form" action="" method="post" name="upload_file" enctype="multipart/form-data"> | |
| {{form.hidden_tag()}} | |
| <!-- userinput --> | |
| <div class="form-group"> | |
| <label>{{ form.name.label }}</label> | |
| {{ form.name(class_='form-control') }} | |
| </div> | |
| <div class="form-group"> | |
| <label>{{ form.img.label }}</label> | |
| {{ form.img(class_='form-control') }} | |
| </div> | |
| <!-- submit --> | |
| <ul class="list-inline"> | |
| <input type="submit" class="btn btn-primary" name="send" value="UPLOAD"> | |
| </ul> | |
| </form> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! python | |
| # -*- coding: utf-8 -*- | |
| @app.route('/upload', methods=['GET', 'POST']) | |
| def upload_form(): | |
| form = UploadForm() | |
| if form.validate_on_submit(): | |
| print form.name.data | |
| # form.img.data contains fileobject | |
| # flag = your_upload_function(form.img.data) | |
| # if flag: print "Upload successful" | |
| flash(u'Upload done') | |
| return render_template('upload.html', form=form) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment