The Flask Mega-Tutorial Rereading - Part 2

This post is part 2 of the "The Flask Mega Tutorial" series:

  1. Running through The Flask Mega-Tutorial Again
  2. The Flask Mega-Tutorial Rereading - Part 2

Table Of Contents

This tutorial1 is really amazing and worth my second learning.

Chapter 7: Error Handling

Debug Mode

$ export FLASK_DEBUG=1
$ flask run

The browser allows you to see the error in detail.

Custom Error Pages

Use the decorator @app.errorhandler.

app/ - Custom error handlers

from flask import render_template
from app import app, db

def not_found_error(error):
    return render_template('404.html'), 404

def internal_error(error):
    return render_template('500.html'), 500

app/ - Import error handlers

# ...

from app import routes, models, errors

Sending Errors by Email - Email configuration

class Config(object):
    # ...
    MAIL_SERVER = os.environ.get('MAIL_SERVER') or "localhost"
    MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25)
    MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
    ADMINS = ['']

app/ - Log errors by email

import logging
from logging.handlers import SMTPHandler

# ...

if not app.debug:
    if app.config['MAIL_SERVER']:
        auth = None
        if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
            auth = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
        secure = None
        if app.config['MAIL_USE_TLS']:
            secure = ()
        mail_handler = SMTPHandler(
            mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
            fromaddr='no-reply@' + app.config['MAIL_SERVER'],
            toaddrs=app.config['ADMINS'], subject='Microblog Failure',
            credentials=auth, secure=secure)

Testing Approach 1: DubuggingServer in python

This is a fake email server that accepts emails, but instead of sending them, it prints them to the console. To run this server, open a second terminal session and run the following command on it:

(venv) $ python -m smtpd -n -c DebuggingServer localhost:8025

Then export MAIL_PORT=8025 before flask run.

Testing Approach 2: Real Email Server

export MAIL_PORT=587
export MAIL_USE_TLS=1
export MAIL_USERNAME=<your-gmail-username>
export MAIL_PASSWORD=<your-gmail-password>

Logging to a File

app/ - Logging to a file

# ...
from logging.handlers import RotatingFileHandler
import os

# ...

if not app.debug:
    # ...

    if not os.path.exists('logs'):
    file_handler = RotatingFileHandler('logs/microblog.log', maxBytes=10240,
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))

    app.logger.setLevel(logging.INFO)'Microblog startup')

Fixing the Duplicate Username Bug

app/ - Validate username in edit profile form.

class EditProfileForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    about_me = TextAreaField('About me', validators=[Length(min=0, max=140)])
    submit = SubmitField('Submit')

    def __init__(self, original_username, *args, **kwargs):
        super(EditProfileForm, self).__init__(*args, **kwargs)
        self.original_username = original_username

    def validate_username(self, username):
        if != self.original_username:
            user = User.query.filter_by(
            if user is not None:
                raise ValidationError('Please use a different username.')

app/ - Validate username in edit profile form.

@app.route('/edit_profile', methods=['GET', 'POST'])
def edit_profile():
    form = EditProfileForm(current_user.username)
    # ...