烧瓶到HTML变量

时间:2020-10-09 13:12:55

标签: python html flask web

我在烧瓶中的代码

@app.route('/index', methods=['GET', 'POST'])
def show_index():   
    #label = 'cracked'
    return render_template('index.html')
    
if __name__ == '__main__':
    # Debug/Development
    app.run(debug=True, host="127.0.0.1", port="5000")    

我的HTML代码:

<html>

  <body>
 <img src="{{url_for('static', filename='img_to_display.jpg')}}" alt={{name}}/>
 <a href="https://maps.google.com/?q=<lat>,<lng>">  
  </body>
  
  
</html>

我想在我的html代码中使用name,lat和lng作为变量,我可以从我的python代码(Flask)中控制..任何帮助。谢谢

1 个答案:

答案 0 :(得分:2)

将值传递到render_template函数中。

@app.route('/index', methods=['GET', 'POST'])
def show_index():   
    #label = 'cracked'
    lat = 0
    lng = 0
    return render_template('index.html', lat=lat, lng=lng)
    
if __name__ == '__main__':
    # Debug/Development
    app.run(debug=True, host="127.0.0.1", port="5000")

然后您可以在模板中访问它们。

<html>

  <body>
 <img src="{{url_for('static', filename='img_to_display.jpg')}}" alt={{name}}/>
 <a href="https://maps.google.com/?q={{ lat }},{{ lng }}">  
  </body>
  
  
</html>