如何告诉 jinja 关于静态子目录中的文件?
How to tell jinja about files in static sub-directories?
我创建了一个网络应用程序,用户可以在其中上传图像到我的网络应用程序,该图像保存在 myproject/static/uploads
目录中,但我无法显示该图像。
我的文件上传代码:
file = request.files['file']
filename = secure_filename(file.filename)
if filename != '':
file_ext = os.path.splitext(filename)[1]
if file_ext not in current_app.config['UPLOAD_EXTENSIONS']:
return 'Invalid Image', 400
file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
post = Post(title=title,body=body,author_id=current_user.id,author=current_user,filename=filename)
db.session.add(post)
db.session.commit()
return redirect(url_for('blog.index'))
return render_template('blog/create.html')
我的索引视图:
@bp.route('/')
def index():
posts = Post.query.all()
files = os.listdir(current_app.config['UPLOAD_FOLDER'])
return render_template('blog/index.html', posts=posts, files=files)
index.html
{% block content %}
{% for post in posts %}
<article class="post">
<header>
<div>
<h1>{{ post.title }}</h1>
<div class="about">by {{ post.author.username }} on {{ post.created.strftime('%Y-%m-%d') }}</div>
</div>
</header>
<p class="body">{{ post.body }}</p>
</article>
{% for file in files %}
<img src="{{ url_for('static', filename='uploads/post.filename') }}" style="width: fit-content;">
{% endfor %}
{% if not loop.last %}
<hr>
{% endif %}
{% endfor %}
{% endblock %}
您必须使用 string concatenation 创建动态文件名:
<img src="{{ url_for('static', filename='uploads/' ~ post.filename) }}" style="width: fit-content;">
您只需在主文件中执行此操作即可:
app = Flask(__name__,
static_url_path='',
static_folder='templates/static',
template_folder='templates')
然后调用您的文件:('example.js' 的路径是 'app/templates/static/js')
<img width="130" src="/js/example.js">
我创建了一个网络应用程序,用户可以在其中上传图像到我的网络应用程序,该图像保存在 myproject/static/uploads
目录中,但我无法显示该图像。
我的文件上传代码:
file = request.files['file']
filename = secure_filename(file.filename)
if filename != '':
file_ext = os.path.splitext(filename)[1]
if file_ext not in current_app.config['UPLOAD_EXTENSIONS']:
return 'Invalid Image', 400
file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
post = Post(title=title,body=body,author_id=current_user.id,author=current_user,filename=filename)
db.session.add(post)
db.session.commit()
return redirect(url_for('blog.index'))
return render_template('blog/create.html')
我的索引视图:
@bp.route('/')
def index():
posts = Post.query.all()
files = os.listdir(current_app.config['UPLOAD_FOLDER'])
return render_template('blog/index.html', posts=posts, files=files)
index.html
{% block content %}
{% for post in posts %}
<article class="post">
<header>
<div>
<h1>{{ post.title }}</h1>
<div class="about">by {{ post.author.username }} on {{ post.created.strftime('%Y-%m-%d') }}</div>
</div>
</header>
<p class="body">{{ post.body }}</p>
</article>
{% for file in files %}
<img src="{{ url_for('static', filename='uploads/post.filename') }}" style="width: fit-content;">
{% endfor %}
{% if not loop.last %}
<hr>
{% endif %}
{% endfor %}
{% endblock %}
您必须使用 string concatenation 创建动态文件名:
<img src="{{ url_for('static', filename='uploads/' ~ post.filename) }}" style="width: fit-content;">
您只需在主文件中执行此操作即可:
app = Flask(__name__,
static_url_path='',
static_folder='templates/static',
template_folder='templates')
然后调用您的文件:('example.js' 的路径是 'app/templates/static/js')
<img width="130" src="/js/example.js">