0

I have header.html:

<body>
    <a class="navbar-brand mr-4 d-flex align-items-center" href="{{ url_for('dash') }}">
        <img class="img-fluid" src="../static/assets/images/live-steam/logo.png" alt="Image">
        <p class="px-1 font-weight-bold font-14">{{sys_domain}}</p>
    </a>
</body>

and .py code:

@flask.route('/header')
def header():
  cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
  cur.execute('SELECT * FROM system_settings')
  user = cur.fetchone()

  sys_domain = (user['system_name'])

  return render_template("header.html", sys_domain=sys_domain)

When i include this header page to another page '{{sys_domain}}' show nothing!

example of page that header.html include to it:

<body>
   <header>
      {% include 'header.html' %}
   </header>
</body>

1 Answers1

0

I believe it is because when you try to use include, it will not call via the flask route. It is including the template directly and rendering it. You can check this official template documentation link

You can use the "with" keyword of jinja2 to call it that way.

You can check this link to have an idea of this.

You can retrieve the user['system_name'] from mysql as sys_domain variable in the route function of .py code from where you are calling the html file in which header.html is to be called. Then you can do something like this.

{% with sys_domain=sys_domain %}
    {% include 'header.html' %}
{% endwith %}
Yash
  • 35
  • 1
  • 5
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30905356) – Flair Jan 29 '22 at 07:09
  • Thank you @Flair I will update my answer. – Yash Jan 31 '22 at 09:38