flask.views View Example Code

View is a class within the flask.views module of the Flask project. View provides an alternative way to use view functions by subclassing this class and implementing dispatch_request for the routing system. This is typically only used in more advanced situations such as extending the Flask source code, rather than a standard way of interacting with the framework.

MethodView and http_method_funcs are a couple of other callables within the flask.views package that also have code examples.

These topics are also useful while reading the View examples:

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 / utils / views.py

# views.py
from flaskbb.utils.helpers import render_template
from flask.views import View


class RenderableView(View):
    def __init__(self, template, view):
        self.template = template
        self.view = view

    def dispatch_request(self, *args, **kwargs):
        view_model = self.view(*args, **kwargs)
        return render_template(self.template, **view_model)



## ... source file continues with no further View examples...

Example 2 from Datadog Flask Example App

The Datadog Flask example app contains many examples of the Flask core functions available to a developer using the web framework.

Datadog Flask Example App / python/flask/app / app.py

# app.py
from ddtrace import patch_all; patch_all(flask=True, requests=True)  # noqa

from ddtrace import tracer

from flask import Flask, Response
from flask import after_this_request
from flask import abort, jsonify, render_template, url_for
from flask.views import View
from werkzeug.routing import Rule

from flask_caching import Cache
from flask_cors import CORS

import requests

from .blueprint import bp
from .exceptions import AppException
from .limiter import limiter
from .signals import connect_signals

app = Flask(__name__)

app.register_blueprint(bp)

connect_signals(app)

CORS(app)
Cache(app, config=dict(CACHE_TYPE='simple'))
limiter.init_app(app)


@app.context_processor


## ... source file abbreviated to get to View examples ...



@app.route('/stream')
def stream():
    def generate():
        for i in range(100):
            yield '{}\n'.format(i)

    return Response(generate(), mimetype='text/plain')


@app.route('/abort/<int:code>')
def abort_endpoint(code):
    abort(code)


@app.errorhandler(404)
def handle_404(e):
    return '404', 404


@app.errorhandler(500)
def handle_500(e):
    return '500', 500


class MyView(View):
    methods = ['GET']

    def dispatch_request(self, name):
        return 'Hello %s!' % name


app.add_url_rule('/hello/<name>', view_func=MyView.as_view('myview'))



## ... source file continues with no further View examples...

Sponsored By

Sentry logo

Software errors are inevitable. Chaos is not. Try Sentry for free.


AssemblyAI logo

The most accurate speech-to-text API. Built for Python developers.

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 Twitter & Facebook.

Matt Makai 2012-2020