我正在练习烧瓶和上传,但遇到了一些问题。首先我下载了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
我将链接附加到我的 GitHub 项目,以便任何读者都能清楚地理解我在说什么。 https://github.com/lorcosta/flask-reuploaded/tree/lab/Labs/Lab4_IS_final
错误似乎来自线路
配置上传(应用程序,照片)
在这个文件'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)
我认为这可能与定义“照片”之前的行有关。为了解决这个问题,我从文档中查找了一个模板,并在 GitHub 上的同一链接的文件“app.py”中找到了您可以看到的内容:
# 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”非常相似,那么为什么我不断收到错误?
在此先感谢大家