图像未显示在浏览器中
Image not showing up in the browser
这是我正在尝试做的事情。我保存在数据库中的图像将转到正确的路径。但是他们没有出现在网站上。
@blogs.route("/post/new", methods=['GET', 'POST'])
def new_post():
if ('user' in session and session['user'] == params["username"]):
form = PostForm()
if form.validate_on_submit():
pic = save_picture(request.files['pic'])
post = Post(title=form.title.data,
content=form.content.data, img=pic)
db.session.add(post)
db.session.commit()
flash('Your post has been created!', 'success')
image_file = url_for('static', filename = 'profile_pics/' + pic)
return render_template('post.html',image_file=image_file)
return render_template('create_post.html', title='New Post',
form=form)
return "please login to the dashboard first. Dont try to enter without logging in!"
HTML那边
<img src="{{image_file}}" alt="error">
这将是路由函数:
image_file = url_for('static', filename='profile_pics/' + post.img)
return render_template('template.html', image_file=image_file)
这是它在模板中的样子:
<img src="{{ image_file }}">
问题可能是您实际上无法在 html 中嵌套变量,尤其是因为 jinja 可能将其解释为文字字符串
已找到修复程序!!
我发现可以使用 python 中的 set 关键字作为变量将 post.img 存储在其中,然后在源代码中引用它。
{% set img_name = 'profile_pics/' + post.img %}
<img src="{{url_for('static', filename = img_name)}}" alt="error">
这是我正在尝试做的事情。我保存在数据库中的图像将转到正确的路径。但是他们没有出现在网站上。
@blogs.route("/post/new", methods=['GET', 'POST'])
def new_post():
if ('user' in session and session['user'] == params["username"]):
form = PostForm()
if form.validate_on_submit():
pic = save_picture(request.files['pic'])
post = Post(title=form.title.data,
content=form.content.data, img=pic)
db.session.add(post)
db.session.commit()
flash('Your post has been created!', 'success')
image_file = url_for('static', filename = 'profile_pics/' + pic)
return render_template('post.html',image_file=image_file)
return render_template('create_post.html', title='New Post',
form=form)
return "please login to the dashboard first. Dont try to enter without logging in!"
HTML那边
<img src="{{image_file}}" alt="error">
这将是路由函数:
image_file = url_for('static', filename='profile_pics/' + post.img)
return render_template('template.html', image_file=image_file)
这是它在模板中的样子:
<img src="{{ image_file }}">
问题可能是您实际上无法在 html 中嵌套变量,尤其是因为 jinja 可能将其解释为文字字符串
已找到修复程序!!
我发现可以使用 python 中的 set 关键字作为变量将 post.img 存储在其中,然后在源代码中引用它。
{% set img_name = 'profile_pics/' + post.img %}
<img src="{{url_for('static', filename = img_name)}}" alt="error">