How to display a variable in HTML

后端 未结 1 544
一生所求
一生所求 2020-12-06 08:57

I am making a web app using Python and have a variable that I want to display on an HTML page. How can I go about doing so? Would using {% VariableName %} in th

1条回答
  •  甜味超标
    2020-12-06 09:29

    This is very clearly explained in the Flask documentation so I recommend that you read it for a full understanding, but here is a very simple example of rendering template variables.

    HTML template file stored in templates/index.html:

    
    
        

    Here is my variable: {{ variable }}

    And the simple Flask app:

    from flask import Flask, render_template
    
    app = Flask('testapp')
    
    @app.route('/')
    def index():
        return render_template('index.html', variable='12345')
    
    if __name__ == '__main__':
        app.run()
    

    Run this script and visit http://127.0.0.1:5000/ in your browser. You should see the value of variable rendered as 12345

    0 讨论(0)
提交回复
热议问题