flask request Example Python Code

The Flask request (source code) object is critical for building web applications with this web framework. The request context allows you to obtain data sent from the client such as a web browser so that you can appropriately handle generating the response.

Example 1 from FlaskBB

FlaskBB (project website) is a Flask-based forum web application. The web app allows users to chat in an open message board or send private messages in plain text or Markdown. FlaskBB is provided as open source under this license.

flaskbb / flaskbb / app.py

# -*- coding: utf-8 -*-
"""
    flaskbb.app
    -----------
    manages the app creation and configuration process
    :copyright: (c) 2014 by the FlaskBB Team.
    :license: BSD, see LICENSE for more details.
"""
import logging
import logging.config
import os
import sys
import time
import warnings
from datetime import datetime

from flask import Flask, request
from flask_login import current_user
from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlalchemy.exc import OperationalError, ProgrammingError


## ... code abbreviated here due to several hundred non-relevant lines ...


def configure_before_handlers(app):
    """Configures the before request handlers."""

    @app.before_request
    def update_lastseen():
        """Updates `lastseen` before every reguest if the user is
        authenticated."""
        if current_user.is_authenticated:
            current_user.lastseen = time_utcnow()
            db.session.add(current_user)
            db.session.commit()

    if app.config["REDIS_ENABLED"]:

        @app.before_request
        def mark_current_user_online():
            if current_user.is_authenticated:
                mark_online(current_user.username)
            else:
                mark_online(request.remote_addr, guest=True)

    app.pluggy.hook.flaskbb_request_processors(app=app)

Sign up for a monthly email with Full Stack Python tutorials. No spam ever.

Learn More Python

Introduction to Ansible video course logo.

Deploy web apps with the Ansible configuration management tool.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via newsletter, Twitter & Facebook.

Matt Makai 2012-2019