Home > Software design >  Changing HTML if flask IF statement is true and display a nav bar
Changing HTML if flask IF statement is true and display a nav bar

Time:04-26

I have a cookie that stores an integer value, I want it: If a user is logged in show a nav bar else hide it. I also need to hide an tag if the cookie exists.

CodePudding user response:

You can do it either way (backend or in html)

Html way

{% if "cookie_name" in request.cookies %}

Code for nav show

{% else %}

Do something

{% endif %}

Backend

In case you might want to do this

@app.route("/your_url")
def your_url():

#you could also compare the value 
    if "name_of_cookie" in request.cookies:
        return render_template("your.html", nav = True)
    else:
        return render_template("your.html", nav = False)

HTML

{% if nav %}

Code for showing nav

{% else %}

Do Something


{% endif %}


  • Related