Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

Flask provides a class named View, which can be inherited to add our custom behavior.

The following is an example of a simple GET request:

from flask.views import View 
 
class GetRequest(View): 
 
    def dispatch_request(self): 
        bar = request.args.get('foo', 'bar') 
        return 'A simple Flask request where foo is %s' % bar 
 
app.add_url_rule( 
    '/a-get-request', view_func=GetRequest.as_view('get_request') 
) 

To accommodate both GET and POST requests, we can write the following code:

from flask.views import View class GetPostRequest(View): methods = ['GET', 'POST'] def dispatch_request(self): if request.method == 'GET': bar = request.args.get('foo', 'bar') if request.method == 'POST': bar = request.form.get('foo', 'bar') return 'A simple Flask ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content