【问题标题】:Passing variable name into {{ }} flask [duplicate]将变量名传递到 {{ }} 烧瓶 [重复]
【发布时间】:2018-03-20 06:56:27
【问题描述】:

我想从数据库中获取图片名称并通过这段代码显示在 html 文件中

 <img src="{{ url_for('static', filename='img/{{rows.infor_image}}') }}" style="width:50%; height:60%;"> 

但我不知道如何用正确的语法编写来显示要显示的图像

我尝试在 .py 文件中声明 image_name 但我不知道如何使用它

@app.route('/herbsinfo/<id>')
def landing_page(id):


cur = mysql.connection.cursor()

#Query
cur.execute('SELECT version()')
result = cur.execute("SELECT * FROM information where infor_id = %s",[id])

rows = cur.fetchone()
image_name = rows['infor_image']
if result > 0:
    return render_template('herbsinfo.html', rows=rows,image_name=image_name)

或者我可以用其他方式显示图像

【问题讨论】:

    标签: html mysql flask jinja2


    【解决方案1】:

    试试这个:

    <img src="{{ url_for('static', filename='img/'+image_name) }}" style="width:50%; height:60%;">
    

    <img src="{{ url_for('static', filename='img/'+rows.infor_image) }}" style="width:50%; height:60%;">
    

    你不需要额外添加{{ }}

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 2017-08-20
      • 2018-04-08
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      相关资源
      最近更新 更多