DEV Community

Cover image for Django vs Flask, what to choose!
Tumusiime Ezra Jnr
Tumusiime Ezra Jnr

Posted on • Updated on

Django vs Flask, what to choose!

Table of contents

Introduction

Hello, hope you are doing well, well, this is 2022 knocking at the door and we need to start doing things its new way it is coming along with.
Whereas there are more than 50 frameworks of Python, there are only two of these that have continuously become the kings of the web, flask and Django at least as far as 2021.
These two frameworks seem to be the most mature and most loved on the web by many beginners, advanced developers and companies. They give us a chance to use the world's best language for full stack development. Let's break them and we get to know which to learn and use when! Can we roll!!

Flask

Background

Flask was created by Armin Ronacher of Pocoo, an international group of Python enthusiasts formed in 2004. According to Ronacher, the idea was originally an April Fool's joke that was popular enough to make into a serious application. The name is a play on the earlier Bottle framework.
When Ronacher and Georg Brandl created a bulletin board system written in Python, the Pocoo projects Werkzeug and Jinja were developed.

Flask's philosophy according to this article is to let the developer take on the driver's seat and have total/full control over the application they are developing.
This has made the framework become the no.1 choice to most of the developers that love doing things their own way.

Flask code sample:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello, World!'
Enter fullscreen mode Exit fullscreen mode

With the above code, you have a full application that outputs Hello, World in the browser running on http://localhost:5000, amazing, right!

Who uses flask

Well, flask being a python light weight framework, most of the companies have picked it to play a big role in building micro-services. As we have seen in the above code snippet, with just one file, you have a fully working app. Therefore, over 900 companies may not entirely be depending on flask as the primary backend framework but include it in their stack.
This list only shows the top 10.

  1. Netflix
  2. Reddit
  3. Lyft
  4. CRED
  5. Trivago
  6. Zalando
  7. MIT
  8. Patreon
  9. ML
  10. Sendcloud

As per this writing, Flask had 2.2k watches, 57.1k stars and 14.7k forks on github, these may have changed by the time you are reading this but you can click here for the current standings

Django

Django (/ˈdʒæŋɡoʊ/ JANG-goh; sometimes stylized as django) is a Python-based free and open-source web framework that follows the model–template–views (MTV) architectural pattern. It is maintained by the Django Software Foundation (DSF), an independent organization established in the US as a 501(c)(3) non-profit.

Background

Django was created in the fall of 2003, when the web programmers at the Lawrence Journal-World newspaper, Adrian Holovaty and Simon Willison, began using Python to build applications. Jacob Kaplan-Moss was hired early in Django's development shortly before Simon Willison's internship ended. It was released publicly under a BSD license in July 2005. This framework has been around for over 16 years atleast as per this writing! The framework was named after guitarist Django Reinhardt.

Who uses django

Well various applications/companies or even tools may not be using Django as the framework but it's counterpart framework Django Rest framework which is a derivative of the same.

The list shows the companies currently using Django in their progressive work. The list only shows the top 10 but there are more.

  1. Disqus
  2. Instagram
  3. Bitbucket
  4. MacArthur Foundation
  5. Mozilla
  6. National Geographic
  7. Open Knowledge Foundation
  8. Pinterest
  9. Open Stack
  10. Spotify

Image description

As per this writing, Django had 2.3k watches, 62.1k stars and 26.5k forks on github, these may have changed by the time you are reading this but you can check the Django repo for the current stand

Similiraties

Well, no matter how they seem to be, the two frameworks share somethings in common which you can not under look. And the matter of the fact, if you are ready comfortable with one, you will find it extremely easy to move one to the next. Like the process is seamless.

Here are the similarities you will find in both.

  1. Python frameworks. Well, without knowledge of python, then you won't be able to write the two frameworks. The are highly Object oriented, and therefore all functions, classes and data structures from python itself, can be used in both.
  2. Fullstack frameworks. Both are independent fullstack frameworks, that is, you will not need any other framework alongside them to develop the backend, frontend etc. They come with inbuilt template engines to handle the front-end and use python to handle the backend.
  3. ORM for models. The frameworks use Object Relational Mappers to provide quick interactions with the database. Whereas Flask does not ship in with this functionality, a special tool to achieve the same has been developed called Flask-SQLAlchamey .On the other hand, the ORM functionality is in-built with django and it's one of those features Django beats most of the other frameworks on.
  4. Request-Response behaviour. With both frameworks you will not miss finding features such as views which will receive requests, perform some logic and map them to the right response. This response maybe returning a new page, some json, or file.
  5. Routes/urls. Whereas the design differs, the functionality does not. These are mappers, which tell the frameworks which view to return on which request. 6.** Sessions and authentication**. Whereas, you may not start the sessions automatically with flask, helper functions for starting, updating, deleting sessions, and performing authentication are provided for by the frameworks. Some of these features can be achieved with some extra libraries in flask, but relying on external libraries in django is upon you. Whichever choice you opt for, you will find it easy in both to handle the functionalities.
  6. In-built template engines. Whereas they may not entirely be the same, the frameworks come with the inbuilt way of building templates(what users see). With these, python functionalities such loops, conditionals, string laterals and many more are presented. Flask uses Jinja2, but with django, you can choose to use Jinja2 but this is not their default. Django has defined its own template engine called django templates, which works closely similar to Jinja2 with little upgrades.

These are not the only similarities, the endless will surely be bigger, but you scan from the above for more...
Next, let's move on how the two differ.

Differences

There will be a few distinctions between the two frameworks, and that's the reason why I had put it that switching from one to another shall really be seamless. Let's see how they differ.

  1. Architecture. Django is MVT(That is, Model-View-Template). Now do not take "View" for how other frameworks of MVC(Model-View-Controller) take it, Model-will still be Model, View will be equivalent to Controller and Template will equivalent to View in MVC. Somehow confusing right!, please read the docs to understand how django handles this feature. Django stores and names these files accordingly and most of them are provided by default. On the other hand, Flask is a modular framework, which means, one file can even contain the whole app. Whereas, the architecture is internally maintained as for django, the structure is entirely upon you.
  2. Flexibility. Django is meant for developers who want to get tasks complete in time. That's why you will find most of the features provided for by default. This may look a good thing and will actually do the right work for you, but this means you will have less freedom of updating the defaults which will require extra knowledge of the framework to update these features. On the other hand, Flask is entirely flexible, ground up. In brief, there is no convention of designing a Flask application. This also looks good for the start, but when the project starts getting complex, new members, and/or even old team may find it hard to update the code.
  3. Scalability. Flask is highly meant to develop modular applications also known as micro services in the same project. This implies that different developers can work on different small modules independent of each other but communicating together. However, django is somehow different. Django itself is considered a project, and then inside it, you set up as many applications as you feel like. This let's django handle even the most complex project with ease. Also the development convention of django while creating these apps makes it very simple for one to divide out members to work on different modules within the same project without necessarily handling them in a distributed format. Also, plugging in a new app is very seamless also and does not affect the other applications ran in the same project.
  4. Security. Whereas in django, starting from the user, and all the forms of authentications such as creating accounts, logging in, resetting and changing passwords, confirming passwords and many more are inbuilt, flask loses out on some of these features and you will end up building them from scratch or depending on third-party libraries. Features like cryptography, click-jacking, CSRF and worked upon by django by default.
  5. Learning curve. If you already know python, Flask should take at most a week to write your first application. This differs with django, most developers have confessed that understanding the file structure of the framework has become an issue, however, understanding this flow will make django equally easier. Therefore, django will require you to at least two weeks to feel comfortable working on a good project.
  6. ORM. Whereas flask depends and works well with Flask-SQLAlchamy, Django's ORM is a killer. From the process of creating models, tracking and squashing, working with more than one database, and changing from one database to the other. All these make Django ORM a killer feature especially when it comes to handling complex databases with very many tables.

Conclusion

With that said and more as you will read from other developers, the question still stands, what do you choose?
Well, I am someone who writes both frameworks and my conclusion is thought about.

When you are handling something that is not complex at all, please choose Flask.

If you are working on a project with some other framework such as Express or Laravel, but wanted some independent feature to be done with python, please choose flask.

Otherwise, choose django.

Top comments (0)