Book Image

Mastering Flask

By : Jack Stouffer
Book Image

Mastering Flask

By: Jack Stouffer

Overview of this book

Starting from a simple Flask app, this book will walk through advanced topics while providing practical examples of the lessons learned. After building a simple Flask app, a proper app structure is demonstrated by transforming the app to use a Model-View-Controller (MVC) architecture. With a scalable structure in hand, the next chapters use Flask extensions to provide extra functionality to the app, including user login and registration, NoSQL querying, a REST API, an admin interface, and more. Next, you’ll discover how to use unit testing to take the guesswork away from making sure the code is performing as it should. The book closes with a discussion of the different platforms that are available to deploy a Flask app on, the pros and cons of each one, and how to deploy on each one.
Table of Contents (20 chapters)
Mastering Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Flask Login


To start using Flask Login, it needs to be downloaded first:

$ pip install flask-login

The main Flask Login object is the LoginManager object. Like the other Flask extensions, initialize the LoginManager object in extensions.py:

from flask.ext.login import LoginManager
…
login_manager = LoginManager()

There are some configuration options that need to be changed on the object:

login_manager.login_view = "main.login"
login_manager.session_protection = "strong"
login_manager.login_message = "Please login to access this page"
login_manager.login_message_category = "info"

@login_manager.user_loader
def load_user(userid):
    from models import User
    return User.query.get(userid)

The preceding configuration values define which view should be treated as the login page and what the message to the user while logging in should look like. Setting the option session_protection to strong better protects against malicious users tampering with their cookies. When a tampered cookie is identified...