"RuntimeError: no destination for set photos" using flask-reuploaded
"RuntimeError: no destination for set photos" using flask-reuploaded
我正在练习 Flask 和上传,但遇到了一些问题。
首先,我下载了 flask-uploads 来管理文件的上传,但我发现这个包有问题,所以我转向 Flask-Reuploaded,一切似乎都正常。
当我 运行 我的应用程序出现 运行 时间错误时,这里是完整的堆栈跟踪:
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
我将 link 附加到我的 GitHub 项目中,因此任何 reader 都可以清楚地理解我在说什么。
https://github.com/lorcosta/flask-reuploaded/tree/lab/Labs/Lab4_IS_final
错误似乎来自行
configure_uploads(app, photos)
在此文件中 '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)
我认为它可能与定义 'photos' 之前的行有关。
为了解决这个问题,我从文档中寻找了一个模板,我发现你可以在文件 'app.py' 中看到与 GitHub:
相同的 link
# 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()
当我 运行 这个模板时,一切正常,我什至可以从浏览器访问并上传保存在本地的文件。因此,如果 'app.py' 有效,这与我的 'run.py' 完全相同,没问题,为什么我一直收到错误?
在此先感谢大家
你的初始配置有错字,你应该把UPLOADED_PHOTO_DEST
改成复数集合UPLOADED_PHOTOS_DEST
:
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd()
我正在练习 Flask 和上传,但遇到了一些问题。 首先,我下载了 flask-uploads 来管理文件的上传,但我发现这个包有问题,所以我转向 Flask-Reuploaded,一切似乎都正常。
当我 运行 我的应用程序出现 运行 时间错误时,这里是完整的堆栈跟踪:
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
我将 link 附加到我的 GitHub 项目中,因此任何 reader 都可以清楚地理解我在说什么。 https://github.com/lorcosta/flask-reuploaded/tree/lab/Labs/Lab4_IS_final
错误似乎来自行
configure_uploads(app, photos)
在此文件中 '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)
我认为它可能与定义 'photos' 之前的行有关。 为了解决这个问题,我从文档中寻找了一个模板,我发现你可以在文件 'app.py' 中看到与 GitHub:
相同的 link# 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()
当我 运行 这个模板时,一切正常,我什至可以从浏览器访问并上传保存在本地的文件。因此,如果 'app.py' 有效,这与我的 'run.py' 完全相同,没问题,为什么我一直收到错误?
在此先感谢大家
你的初始配置有错字,你应该把UPLOADED_PHOTO_DEST
改成复数集合UPLOADED_PHOTOS_DEST
:
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd()