【发布时间】:2014-05-18 23:55:46
【问题描述】:
你好,对不起(这里是编程新手)!我正在使用 Flask 和 SQLalchemy 和 postgresql 创建一个站点,用户可以在其中上传图像。到目前为止,我一直在弄清楚如何在 openshift 数据目录中实现或创建一个文件夹,该文件夹将存储这些上传的图像。此外,我还在研究如何将这些图像定向到在 openshift 中创建的文件夹。我正在修补我在互联网上看到的零碎代码。下面是我从在线示例中使用的代码,该代码在上传到 openshift 时会导致问题。
app.config['UPLOAD_FOLDER'] = '/static/img/'
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/test')
def test():
return render_template('test.html')
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to
# the upload folder we setup
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
return redirect(url_for('uploaded_file',
filename=filename))
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
该站点在定位保存上传图像的路径时遇到错误(因此将其调回以显示)。如果有人可以就我如何实现某种关系以将用户帐户(其数据已存储在 POSTgresql 数据库中)链接到他上传的图像提供任何提示,我将不胜感激。请尽量避免使用繁重的编码术语,因为我是一个自学编码的新手。谢谢一百万!
尊敬的, 最大
【问题讨论】:
标签: postgresql flask openshift flask-sqlalchemy