在Flask Peewee上传文件

时间:2013-10-28 08:36:24

标签: python html flask

我想上传一个装有peewee的文件。我有一个代码,但它生成“IOError:[Errno 2]没有这样的文件或目录:'/ lookbook/uploads/1375134_766940759988081_2093520123_n.jpg'”错误。 表格: -

 <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>

View.py: -

from werkzeug import secure_filename
import os
UPLOAD_FOLDER = '/lookbook/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/up', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        filename = secure_filename(file.filename)
        #return filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return redirect(url_for('up',filename=filename))
        return 'hello'
    else:
        return  render_template('fileup.html');

请各位帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

根据错误消息,似乎目录/lookbook/uploads不存在。

>>> open('/tmp/no-such-directory/a-text-file.txt', 'w')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '/tmp/no-such-directory/a-text-file.txt'
>>> os.mkdir('/tmp/no-such-directory')
>>> open('/tmp/no-such-directory/a-text-file.txt', 'w')
<open file '/tmp/no-such-directory/a-text-file.txt', mode 'w' at 0x1ec6270>

确保目录/tmp/no-such-directory存在..