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.