While we can use decorators to wrap around functions for access-control, we can also use our Jinja templating logic to control views. For example, right now, when we log in to our website, we still have a "login" button at the top right.
We should really have the login button go away, same with the register button, and then add a "logout" button.
We can actually handle all of this within our HTML file, so let's visit our header.html
file.
Find the code associated with the navbar, and then replace with some logic:
<ul class="nav navbar-nav navbar-right"> <div style="margin-right: 10px; margin-left: 15px; margin-top: 5px; margin-bottom: 5px;" class="container-fluid"> <h5> {% if session.logged_in %} <a href="/support-donate/"> <span class="glyphicon glyphicon-heart"></span> Support </a> <a href="/logout/"><span class="glyphicon glyphicon-log-out"></span> Logout </a> {% else %} <a href="/support-donate/"> <span class="glyphicon glyphicon-heart"></span> Support </a> <a href="/login/"><span class="glyphicon glyphicon-log-in"></span> Login </a> <a href="/register/"><span class="glyphicon glyphicon-pencil"></span> Sign up</a> {% endif %} </div> </h5> </div> </ul>
Two things to note: We are clearly able to reference the session, and we have not needed to "import" it in our template. Logic like this in your HTML can throw the default tag recognition / highlighting for a loop!
The Jinja templating basically has a "from flask import *" going on behind the scenes. You are able to reference any Flask stuff in the templates without needing to import it in the template, nor your script.
As you build HTML logic like this, especially when it affects what the user sees, you may find that it disrupts the natural highlighting, and your editor may flag things like tags not being closed, etc. Use your better judgement. Sometimes, if the content is thick enough and is a sort of if/else like the above, I will just delete each of the chunks, one at a time, and check to make sure everything is connected right.
No matter what, however, it can be confusing. Just watch out for it, especially if you let your editor automatically close or move tags about.