Hello, I use flask and python 3.6 and I want to populate a menu list with data from MySQL database. I use a template.html file where I want the list of links to appear. I want it to work similar to the @app.route() 'thing' but I don't want a page dedicated to the menu. I want it to appear on every page that uses the template.html file.
class Menu(db.Model):
__tablename__ = "menu"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(4096))
link = db.Column(db.String(4096))
weight = db.Column(db.String(4096))
description = db.Column(db.String(4096))
@app.route('/menu1')
def menu1():
return render_template("menu1.html", menuItems=Menu.query.all())
The code above, from my flask_app.py file, renders a page for the menu to be displayed on but that's not what I want to achieve. Below is my template.html file. I want the menu list to appear in the <head> section.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<!-- ***THIS IS WHERE I WANT MY MENU LIST TO APPEAR. THE CODE IN THE {%block head %} BELOW DOES NOT WORK NOW BUT I PASTE IT THERE TO GIVE A CLEAR EXAMPLE OF WHAT I WANT TO ACHIEVE***
{% block head %}
<ul class="Menu">
{% for item in menuItems %}
<li>
<a href="{{ item.link }}">{{ item.name }}</a>
</li>
{% endfor %}
</ul>
{% endblock %} -->
</head>
<body>
<div class="container">
{% block content %}
{% endblock %}
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }}
{% endif %}
</div> <!-- container -->
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<!-- <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> -->
</body>
</html>
I'm grateful for any advise or link to a guide that gets me closer to achieving my goal since I've been stuck on this for a while.