我试图获取上传文件夹路径。
UPLOAD_FOLDER = '/uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
我在路径中上传图片:
.
└── uploads
└── img_articles
└── 2017
└── 02
└── image.jpg
我想在Jinja模板中使用image.jpg
{% extends "layout.html" %}
{% block content %}
<div class="container">
<img src="{{image_path}}" alt="">
</div>
{% endblock %}
我该怎么办?
答案 0 :(得分:3)
查看专业人士信息中的Uploading Files Pattern。
定义上传路线:
from flask import send_from_directory
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
在模板中,您可以使用路线获取文件名
<img src="{{ url_for('uploaded_file', filename='yourimage.jpg') }}">