Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditional visibility with jinja

I have sort of a slideshow style website using HTML and flask, with each page featuring a previous and next button to toggle different pages. Since I want this to be dynamic, I use session objects. My problem, though, is that for the first and second slides, I don’t want the previous button to be there. I did some researching and found out I can use jinja to conditionally show the button, but I can’t get it to work.

 {% if session["page_index"] == 0 || session["page_index"] ==  1%}
                <div>
  {# Don't show previous button here #}
                </div>
                {% else %}
        <a id="previous" href="/{{session["slides"][session["page"]-1]  }}" class="buttons">Previous</a>
            {% endif %}

The session object code inside the a tag has a red underline on the "-1," even though it works, and the "||" gives an error also. I’m a little new to coding so any help would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’m a little confused by your question. There is one actual issue: the "||" symbol is not used in python to express an or statement, the word "or" is used. As for making the button conditionally visible, you don’t need to have any code in the if statement, and it won’t appear. If for some reason you still want the button to have a hitbox and functionality, you can just change the opacity.

 {% if session["page_index"] == 0 or session["page_index"] ==  1%}
                <div style="opacity: 0%">
    <a id="previous" href="/{{session["slides"][session["page"]-1]  }}" class="buttons">Previous</a>
                </div>
                {% else %}
        <a id="previous" href="/{{session["slides"][session["page"]-1]  }}" class="buttons">Previous</a>
            {% endif %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading