M
M
Morrowind2021-04-03 22:45:43
Flask
Morrowind, 2021-04-03 22:45:43

How to display a Python (flask) variable in an HTML file?

I need to transfer the value of the hook variable from the python code to the html code. I don't understand how to use the syntax in it. An example of an attempt:

app = Flask(__name__)

@app.route('/')
def hello_world():
    hook = 'aaaaaaa!'
    return render_template('index.html')


<p class="lead">  {% if message %}
        <p>{{ form.hook }}</p>
            {% endif %}</p>

            {% if message %}
        <p>{{ hook }}</p>
            {% endif %}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karbivnichy, 2021-04-03
@hekkaaa

I don't understand how to use the syntax in it.

It uses the Jinja2 template engine. Read about him.
The simplest example:
main.py
from flask import render_template
from flask import Flask

app = Flask(__name__) 

@app.route('/')
def index():
    name = 'Бендер Сгибальщик Родригес'
    return render_template('index.html',name=name)

app.run(host='0.0.0.0', port=5000,debug=True)

templates/index.html
<html>
    <head>
        <title>Футурама</title>
    </head>
    <body>
        <h2>Привет, {{name}}!</h2>
    </body>
</html>

S
Sanya Hihi Haha, 2021-04-03
@ValarMayar

stack

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question