Home > front end >  Injecting data into html using Flask
Injecting data into html using Flask

Time:02-24

I have a flask app, about saving strings into some db files. I have a base.html file which is like navbar which i extend to every page. That navbar has a lots of links which require a specific string that the user has to enter, so i wanna know if there's a way to inject strings into that base.html file, cuz i can't make a route for a navbar base file right?

Navbar base file down below

<!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.0">
    <link rel="stylesheet" href="/static/css/base.css">
    <title>
        BukkitList - {% block title %}{% endblock %}
    </title>
</head>
<body>

    <div  id="MENU">
        <div >
            <a href="/"><img src="/static/assets/img/cube.png" alt=""></a>
            <a  href="/">Home</a>
        </div>
        <div >
            <a href="/List{{UserId}}"><img src="/static/assets/img/list.png" alt=""></a>
            <a  href="/List">List</a>
        </div>
        <div >
            <a href="/Task_Add/{{UserId}}"><img src="/static/assets/img/add.png" alt=""></a>
            <a >Add Task</a>
        </div>
        <div >
            <a href=""><img src="/static/assets/img/settings.png" alt=""></a>
            <a >Settings</a>
        </div>
    </div>

    <div  id="NAV">
        <img src="/static/assets/img/cube.png" alt="">
        <h3>{% block navtitle %}
                
            {% endblock %}
        </h3>
        <img src="/static/assets/img/menu.png" alt="" onclick="Menu()">
    </div>

    
    {% block main %}
        
    {% endblock %}
</body>
<script src="/static/js/base.js"></script>
</html>

Yes i need that UserId to be injected.

CodePudding user response:

the question is not very understandable of where the user is inputting the {{UserID}} but from what I understand that there is that userID that you can select from the db in the Python file and you want to pass it into the HTML page or if you have a sign-in in your page, you can grab that ID when they sign in using flask_session either way if you need to pass that userID from the Python file you will need to include it in your return, so in python it will look like that if you are using session:

@app.route("/")
def main():
    UserIDpy = Session["YourSessionVar"]
    return render_template("YourHTMLpage.html", UserID = UserIDpy)

The UserID is the var name that will be passed into the HTML page and UserIDpy is the var name that what UserID saved at.

So that code will replace all of {{ UserID }} you have at you HTML page

CodePudding user response:

I believe you can do this with Flask's session variable. It allows you to create and update a global variable that can be referenced in templates even when you don't render them directly. This is similar to Lychas' answer, but should be more suited for your purpose.
Create/update a session variable in your login route (or wherever you want to update this value) with this line:

session['UserId'] = your_id_value_here

You can then use this session variable in your jinja templates with something like the following:

<a >Add Task</a>

(Note, if you are not already using session, you will need to import it with from Flask import session.)

  • Related