Python Web Applications With Flask – Part II

By realpython - 2020-12-02

Description

In this part of our series on Building a Web Application with Flask we'll set up user accounts, templates, and static files.

Summary

  • Welcome back to the Flask-Tracking development series!
  • flask_tracking/auth.pyfromflask.ext.loginimportLoginManagerfromflask_tracking.users.modelsimportUserlogin_manager=LoginManager()login_manager.login_view="users.login"# We have not created the users.login view yet# but that is the name that we will use for our# login view, so we will set it now.
  • @login_manager.user_loaderdefload_user(user_id):returnUser.query.get(user_id) @login_manager.user_loader registers our load_user function with Flask-Login so that when a user returns after logging in Flask-Login can load the user from the user_id that it stores in Flask’s session.
  • If the user is creating a new site, we create a new site (using the method defined by our CRUDMixin, so if you are making changes to the code yourself, you will want to make sure that Site and Visit both inherit from CRUDMixin) and redirect back to the same page.
  • Return to the view, all this view is doing is copying info from the request down and storing it in the database.

 

Topics

  1. UX (0.48)
  2. Backend (0.15)
  3. Database (0.11)

Similar Articles

How to Build a Python GUI Application With wxPython

By realpython - 2020-12-14

In this step-by-step tutorial, you'll learn how to create a cross-platform graphical user interface (GUI) using Python and the wxPython toolkit. A graphical user interface is an application that has b ...

Spring Data Neo4j - Developer Guides

By Neo4j Graph Database Platform - 2021-01-05

For Java developers who use the Spring Framework or Spring Boot and want to take advantage of reactive development principles, this guide introduces Spring integration through the Spring Data Neo4j pr ...