0% found this document useful (0 votes)
1K views1 page

Flask: The Cheat Sheet: Flask For Django Users

This document provides a cheat sheet comparing common actions and functionality between Django and Flask web frameworks. It lists actions like retrieving query parameters and form data, checking the request type, using sessions, and accessing settings. It also summarizes how to set up a basic "Hello World" app, use URL routing, render templates, and structure apps with Flask. The document provides useful references for Flask documentation and examples to learn more.

Uploaded by

daveix3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views1 page

Flask: The Cheat Sheet: Flask For Django Users

This document provides a cheat sheet comparing common actions and functionality between Django and Flask web frameworks. It lists actions like retrieving query parameters and form data, checking the request type, using sessions, and accessing settings. It also summarizes how to set up a basic "Hello World" app, use URL routing, render templates, and structure apps with Flask. The document provides useful references for Flask documentation and examples to learn more.

Uploaded by

daveix3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Flask for Django users

Flask: The
Action Django Flask
Cheat Sheet Retrieve query params
(“GET data”)
request.GET
request.GET[‘name’]
request.args
request.args[‘name’]

Installation Retrieve form params request.POST request.form


(“POST data”) request.POST[‘first_name’] request.form[‘name’]

$ pip install Flask Checking request type request.method request.method


Retrieve cookie value request.COOKIES request.cookies
Set a cookie response.set_cookie(key, value, ...) response.set_cookie(key, value, ...)
Hello World
Sessions request.session[‘foo’] session[‘foo’]
# myapp.py
Access settings from django.conf import settings app.config
from flask import Flask
settings.my_name app.config[‘my_name’]
app = Flask(__name__)

@app.route(‘/’) Useful functionality


def hello_world():
return ‘Hello World’ From flask import.. This is a... usage
Flask Flask application object app = Flask(__name__)
if __name__ == ‘__main__’:
app.run(debug=True) request.args[‘test’]
request Thread-local request object
request.form[‘name’]
session Thread-local session object session[‘name’] = ‘value’
URL Routing
url_for Builds URLs based on route names url_for(‘blog’, id=45, slug=‘hi’)
@app.route(‘/foo/<name>/<int:age>’) redirect Generates a redirect response return redirect(‘/hello’)
def view(name, age):
return ‘%s is %d years old’ % (
name, age
Basic App Layout Package App Layout
)
More Info
my_app.py my_app/
Template Rendering static/ __init__.py
logo.png application.py • http://flask.pocoo.org (main site & docs)

from flask import render_template base.css models.py • http://flask.pocoo.org/docs/quickstart


templates/ static/ • http://github.com/mitsuhiko/flask
blog_post.html logo.png
return render_template( index.html base.css
‘foo.html’, templates/
var1=value1, var2=value2, ... blog_post.html
) index.html

You might also like