Dynamic user-based content Flask Tutorial




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.

The next tutorial:





  • Introduction to Practical Flask
  • Basic Flask Website tutorial
  • Flask with Bootstrap and Jinja Templating
  • Starting our Website home page with Flask Tutorial
  • Improving the Home Page Flask Tutorial
  • Finishing the Home Page Flask Tutorial
  • Dynamic User Dashboard Flask Tutorial
  • Content Management Beginnings Flask Tutorial
  • Error Handling with Flask Tutorial
  • Flask Flash function Tutorial
  • Users with Flask intro Tutorial
  • Handling POST and GET Requests with Flask Tutorial
  • Creating MySQL database and table Flask Tutorial
  • Connecting to MySQL database with MySQLdb Flask Tutorial
  • User Registration Form Flask Tutorial
  • Flask Registration Code Tutorial
  • Finishing User Registration Flask Tutorial
  • Password Hashing with Flask Tutorial
  • Flask User Login System Tutorial
  • Decorators - Login_Required pages Flask Tutorial
  • Dynamic user-based content Flask Tutorial
  • More on Content Management Flask Tutorial
  • Flask CMS Concluded Flask Tutorial
  • The Crontab Flask Tutorial
  • Flask SEO Tutorial
  • Flask Includes Tutorial
  • Jinja Templating Tutorial
  • Flask URL Converters Tutorial
  • Flask-Mail Tutorial for email with Flask
  • Return Files with Flask send_file Tutorial
  • Protected Directories with Flask Tutorial
  • jQuery with Flask Tutorial
  • Pygal SVG graphs with Flask Tutorial
  • PayPal with Flask Web Development Tutorial
  • Securing your Flask website with SSL for HTTPS using Lets Encrypt