1

I'm practicing with flask and uploads and I'm encountering some issues. Firts I downloaded flask-uploads to manage uploads of file but I figured out that this package have problem so I moved to Flask-Reuploaded and everything seems working fine.

When I run my application I get a runtime error, here's the full stack trace:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask/__main__.py", line 15, in <module>
    main(as_module=True)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask/cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/click/decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask/cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask/cli.py", line 305, in __init__
    self._load_unlocked()
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/run.py", line 26, in <module>
    configure_uploads(app, photos)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask_uploads.py", line 241, in configure_uploads
    config = config_for_set(uset, app, defaults)
  File "/Users/lorenzocosta/Documents/Information System/Labs/Lab4_IS_final/venv/lib/python2.7/site-packages/flask_uploads.py", line 209, in config_for_set
    raise RuntimeError("no destination for set %s" % uset.name)
RuntimeError: no destination for set photos

I attach the link to my GitHub project so it's clear for any reader to understand what I'm speaking about. https://github.com/lorcosta/flask-reuploaded/tree/lab/Labs/Lab4_IS_final

The error seems to come from line

configure_uploads(app, photos)

in this file 'run.py':

# this is run.py and does not work
from flask import Flask, session, render_template
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
import os

from app import UploadForm

app = Flask(__name__)
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SECRET_KEY'] = 'qwertyuiopasdfghjklzxcvbnm'
app.config['MAIL_SERVER'] = 'smtp.mail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ['EMAIL_USERNAME']
app.config['MAIL_PASSWORD'] = os.environ['EMAIL_PASSWORD']
app.config['UPLOADED_PHOTO_DEST'] = os.getcwd()
print os.getcwd()
mail = Mail(app)
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)# the error seems to be here, something related to photos
patch_request_class(app)


if __name__ == '__main__':
    from routes import apply_routing
    apply_routing(app).run(debug=True)

and I think it could be something related to the line before when 'photos' is defined. To solve I looked for a template from documentation and I found what you can see in file 'app.py' at the same link on GitHub:

# this is app.py and works properly
import os
from flask import Flask, render_template
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired, FileAllowed
from wtforms import SubmitField

app = Flask(__name__)
app.config['SECRET_KEY'] = 'I have a dream'
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd()

photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app)  # set maximum file size, default is 16MB


class UploadForm(FlaskForm):
    photo = FileField(validators=[FileAllowed(photos, u'Image only!'), FileRequired(u'File was empty!')])
    submit = SubmitField(u'Upload')


@app.route('/', methods=['GET', 'POST'])
def upload_file():
    form = UploadForm()
    if form.validate_on_submit():
        filename = photos.save(form.photo.data)
        file_url = photos.url(filename)
    else:
        file_url = None
    return render_template('index.html', form=form, file_url=file_url)


if __name__ == '__main__':
    app.run()

when I run this template everything works and I can even access from the browser and upload file which are saved locally. So if 'app.py' works, which is pretty identical to my 'run.py', with no problem why I keep getting the error?

Thanks in advance to everyone

Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37
lorenzo
  • 21
  • 5

1 Answers1

4

There is a typo in your initial configuration, you should change UPLOADED_PHOTO_DEST to a plural of the set UPLOADED_PHOTOS_DEST:

app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd()
Nikolay Shebanov
  • 1,363
  • 19
  • 33