Home  >  Q&A  >  body text

python - Flask-Uploads 上传文件报错

form 代码如下:

from flask.ext.uploads import UploadSet, IMAGES
from flask_wtf import Form
from flask_wtf.file import FileField, FileAllowed, FileRequired

images = UploadSet('images', IMAGES)

class UploadForm(Form):
    upload = FileField('image', validators=[
        FileRequired(),
        FileAllowed(images, 'Images only!')
    ])

以上代码是完全按照flask_wtf的手册来写的。这样写之后,上传文件的时候报以下错误: RuntimeError: cannot access configuration outside request

难道我还需要在其他地方做什么设置么?谢谢。

天蓬老师天蓬老师2742 days ago853

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 11:47:32

    I feel like the problem is not in the code you listed. RuntimeError: cannot access configuration outside request means that the setting items can only be accessed during the processing of user requests, and cannot be accessed at other times (such as when initializing the application)

    reply
    0
  • PHPz

    PHPz2017-04-17 11:47:32

    Solved. flask-uploads needs to add configuration. Just add the following record to the flask configuration file:

    UPLOADED_IMAGES_DEST = 'some/path/'
    

    reply
    0
  • Cancelreply