An In-Depth Comparison – Flask vs Django

An In-Depth Comparison – Flask vs Django

According to the 2019 Stack Overflow developer survey, Python has risen in the ranks of programming languages. This popularity is not only due to applications in artificial intelligence or big data. Python can be used to build server-side web applications.

According to the 2019 Stack Overflow developer survey, Python has risen in the ranks of programming languages. This popularity is not only due to applications in artificial intelligence or big data. Python can be used to build server-side web applications.

Both Django and Flask are hugely popular as Python Web frameworks. In this article, we’ll compare two widely used Python Web Frameworks.

(Note: I’m a big fan of another Python Web Framework called Masonite, so I’ll keep this comparison as objective as possible though).

Framework Installations

Both frameworks can be installed with the package-management system pip.

Flask installation

Flask installation is as simple as below:

pip install Flask touch app.py

BashCopy

Put the source-code below into app.py:

from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!"

PythonCopy

You can run your app with this command:

FLASK_APP=app.py flask run

That’s it! Your Flask application is available on URL http://localhost:5000/.

Django installation

You need to scaffold a bunch of files to start.

pip install Django django-admin startproject django_project python manage.py runserver

BashCopy

Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser. You’ll see a “Congratulations!” page.

Project layout

Django provides a conventional project structure with several applications. But you should create your own project’s structure when using Flask.

Framework Configurations

Flask Configurations

The config attribute of the Flask object is as a config object. For example:

# app.py app = Flask(__name__) app.config['TESTING'] = True

PythonCopy

Django Configurations

A Django settings file contains all the configuration of your Django installation. Here are a couple of example settings:

# settings.py TESTING = True

PythonCopy

Routing

Flask Routing

Flask provides a route() decorator to tell Flask what URL should trigger a function:

# app.py @app.route('/') def hello_world(): return 'Hello, World!'

PythonCopy

Django Routing

Django lets you design URLs however you want:

# urls.py from django.urls import path from . import views urlpatterns = [ path('/', views.home), ]

PythonCopy

Templates

Django ships with a template engine called Django Template Languageand Flask ships with Jinja. Both template engines implement two main features: Template inheritance, Blocks or sections, if/else, for-loops.

Object Relational Mapping (ORM)

Django web framework includes a default ORM that can be used to interact with the application database.

User.objects.all()

Flask does not come with ORM capabilities by default. You need to manually add it if you want. Many Flask Developers prefer SQLAlchemy for database access.

User.query.all()

Admin Interface

Django provides a functional admin module automatically based on your project models. The developers even have the option to customize the admin interface to meet specific business requirements.

Community

Several popular websites use Django: Mozilla, Instagram, The Washington Times and so on. Flask is used by Pinterest or LinkedIn.

REST API

With the JavaScript’s frameworks popularity, REST APIs can be used to perform requests and receive responses via HTTP.

REST API in Flask

There are many Flask extensions to help you build RESTful services: Flask-RESTful for example. But you can use what Flask provides itself:

# app.py from flask import Flask, jsonify app = Flask(__name__) todos = [ { 'name': 'Learn Flask' }, { 'name': 'Learn SQLAlchemy' } ] @app.route('/todos', methods=['GET']) def get_todos(): return jsonify({'todos': todos})

PythonCopy

REST API in Django

Django Rest Framework (or simply DRF) is a powerful module for building web APIs using Django. It’s very easy to build model-backed APIs that have authentication policies and are browsable.

Django or Flask?

There’s no clear winner between Django and Flask, as everything depends on your final goal. Flask and Django both have their strengths and weaknesses. Django is very complete, with regard to ORM, admin interface etc. It’s well documented. But Django has a steep learning curve. Flask might be a better choice because you can learn it fast.

Further reading:

Speed Up Your Python Code with Cython

Using Twitter With Python and Tweepy

An introduction to Heartrate library

Writing Your First Kubernetes Operator with Python and SDK

3 techniques for Python Inversion of Control

django python flask

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

top 30 Python Tips and Tricks for Beginners

In this post, we'll learn top 30 Python Tips and Tricks for Beginners

Lambda, Map, Filter functions in python

You can learn how to use Lambda,Map,Filter function in python with Advance code examples. Please read this article

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

Django admin full Customization step by step

Django admin full Customization step by step. Did you learn about it? Read our article now.

Django vs Flask: Difference Between Django and Flask [Which is Better?]

Django and Flask are two of the best web development frameworks in Python. Practice it and check it out.