codete choosing the best framework for python flask vs django main c38dd0dbbe
Codete Blog

Choosing the Best Framework for Python – Flask vs Django

avatar male f667854eaa

02/06/2021 |

11 min read

Piotr Wawryka

Everybody's been there. You're going along your skill development path, you understood the basics of Python, and one day you decided to do the actual coding. Then, after some solid web research, you have finally faced the dilemma: the Flask vs Django comparison.

Both are ridiculously popular, both have strong communities, and both - if poorly chosen - can break your project into a million pieces. And make you spend even more hours fixing all those bugs or rewriting the code over and over again.

So, what should you know before picking one over the other? 

  1. What is Django?
  2. What is Flask?
  3. Pros and cons of Flask & Django
  4. Which framework is best for your app - Flask vs Django

 

Flask vs Django - the definitions

What is Django?

“The Web Framework for perfectionists with deadlines”

– Django’s tagline

Django is a high-level Python web framework for developing complex database-driven websites and high-quality APIs. Based on a batteries-included approach, it spares you much of the hassle of web development. This way, you can actually focus on writing your app without reinventing the wheel - all the functionality you need is already there.

Django gives you an all-inclusive experience: you get an admin panel, database interfaces, an ORM, and a directory structure for your apps and projects straight out of the box. Its goal is to make the development process smooth and time-efficient, encouraging rapid development and clean, pragmatic design. Plus, it's free and open-source.

It's used on sites like Instagram, Pinterest, Udemy, and The Washington Times.

Read: 10 Django Packages Every Developer Must Know

Alright, so...what is Flask?

“Web development, one drop at a time”

– Flask’s tagline

No surprises here, it's also a framework written in Python. Flask is classified as a microframework, offering the bare minimum of tools to get your app running.

It's unopinionated and leaves it up to you to decide how you want to implement things in your projects. So, if you're thinking of adding open authentication systems, object-relational mappers, upload handling, or form validation to your project, you'll need to find (and install) a third-party plugin that provides a tool to do it.

Depending on your situation, this can be either good or bad. The Flask framework does support a variety of extensions whose functionality (combined) is almost identical to Django. Still, you are forced to plan the whole system of logic, design, and organization by yourself.

Lack of coding experience can lead to big problems here. But if you already have some skills, you can use them freely, following the example of Reddit, Netflix, LinkedIn, and Pinterest developers.

You’ve got the basics. Now, it's time for you to realize something. 

 

Flask vs Django - a serious talk about pros & cons 

Usually referred to as a competitor to Flask, Django is paired with it just too often. In fact, comparing Flask to Django is like comparing a family van to a delivery truck. Although both are called "cars" and do almost the same thing (travel, transporting things, etc.) - one is more suited for carrying people and the other for heavy cargo (e.g. furniture). 

This is just a different weight category. 

So if you're still struggling with whether Django or Flask is better for your project, keep in mind that databases are accessed via Python, not the framework. As in the example above, discussing which is better is simply pointless without context. 

Keeping that in mind let’s have a peek at the most important pros and cons of both frameworks:

 

DJANGO

FLASK

PROS
  • Mature, with a large community.
  • Comprehensive documentation, full of easy to use, step-by-step tutorials.
  • Built-in templating system, admin panel, session & user management, database migration system.
  • Custom ORM that works with most SQL databases.
  • A lot of useful features that you can easily enable in your configuration.
  • Django REST Framework - extension to Django that provides many features for building REST APIs.
  • Wagtail - Django extension that provides CMS functionality.
  • You can integrate Solr/Elasticsearch with Django Haystack.
  • Django Channels - allows you to go beyond HTTP - WebSockets, chat, and IoT protocols.
  • Many Python extensions provide a dedicated SDK for Django: Celery, Sentry, etc.
  • There is already a lot of pre-built code in Django that makes your job a lot easier.
  • It can protect your website or app from most attacks like SQL injection, CSRF, XSS, clickjacking, and many others.
  • Lightweight Framework. Think of Flask like a Lego set, you plug in the pieces to build your application.
  • An extensible framework with a customizable structure. A developer can create a backend system however they want.
  • Can be easily used with any type of database or data store: with raw drivers or ORM such as SQLAlchemy.
  • Easy to start. If you want to learn something you can build quickly, you should use Flask.
  • Can be deployed to serverless platforms using the Serverless framework.
  • Plotly Dash extension that can be used to create beautiful interactive dashboards and charts.
  • Great framework for rapid prototyping, especially for APIs and microservices.
CONS
  • Many built-in functions rely on a (built-in) ORM that only works with SQL databases.
  • The "out of the box" structure makes it a bit difficult when you need specific function changes.
  • If you need something extremely simple without overwhelming performance, you need to turn off the default-enabled features.
  • If you get comfortable with the program, it will do a lot of the heavy lifting for you, but there's also a lot to learn.
  • It doesn't do any calculations for you - if you want the site to be interactive, you'll need to use some JavaScript magic.
  • Harder to maintain as the project grows.
  • If you build a standard CRUD website based on a SQL database, you might end up wasting time on implementing several plugins to rebuild the functionality, offered by Django straight out of the box.
  • It relies on external packages for things like login, security, administration, and forms to compensate for what it lacks.

 

Codete’s insight: which framework is best for your app - Flask vs Django

Again, virtually any project can be based on Flask or Django. They both scale well. In fact, Django can even be stripped down and we practically get something like Flask. Once again, it's important to figure out which framework works in our favor, i.e. which one allows you to not reinvent the wheel and make the project faster:

In the case of Flask, which provides only basic functionality, you can add a lot in the form of extensions. Flask tends to ask you to take shortcuts that will hurt the app's ability to grow. This could be a big red flag, because (in theory), the more dependencies, the worse.

*Remember, these plugins are usually created by independent developers, and you can easily fall into a trap of relying on an extension that is no longer being developed. As I said before, Flask doesn't impose a project structure like Django and gives a lot of freedom. Use it wisely and always double-check your resources.

In the case of Django, I usually check if we really need a "default setup" in the project. If yes, and I'm sure we don't want to migrate the databases to some custom Big Data engine in the future, then Django helps us a lot. In theory, you don't have to use the Django ORM, but a lot of the built-in functionality is based on it in practice.

TL;DR: Django comes with a lot of features, but you need to know how to use them (and generally know a thing or two about Python). Flask is more barebones, meaning you have to build the functions yourself.

 

Let’s work in Python: Django vs Flask vs type of your project

1. Prototypes

It depends. As I mentioned before, it's a reasonable choice if we see that we can benefit from what Django offers and we are sure that the underlying type of the database will not be changed in the future. Especially if we see that we can use the administration panel, the built-in user system, or CMS extensions like Wagtail.

If there is uncertainty about the future design of the system and 3rd party services, it's safer to use Flask and decouple integration with them from application functions. These parts can be reimplemented later with other libraries/tools.

A good example of a service implemented for one of our clients recently is an API implemented in Flask but deployed as a Lambda function using the Serverless framework. We calculated the running costs for the EC2 instance and compared them to the AWS API Gateway solution with Lambda integration. The Serverless solution was cheaper, but in the future, if we want to switch back to EC2 (e.g. because of Lambda limits), we can easily do that without rewriting a single line of code.

2. Web/API interface for non-web application

Mostly Flask. If you have an existing code base and just need a simple interface, Flask will do the job. For example, if you need to expose your Machine Learning model, you can easily do that with Flask. Again, the Flask framework is very quick to prototype and the sites we create are usually performant and easy to maintain.

Flask is also a good choice when SQL can't be used - if, for some reason, you've decided to trade several nights of sleep for hundreds of hours of coding by not using the SQL database.

3. CRUD apps 

Mostly Django because you can use the built-in features and third-party extensions like Django REST Framework, Wagtail, etc. Ironically, you can build apps much faster this way than in lightweight Flask.

As always, it depends on what exactly you have in mind, but in my opinion, Django is worth a try because of its proven admin interface. That alone will probably save you a lot more work than any libraries Flask has to offer. Plus, Django includes many libraries for authentication and many other purposes like django-allauth, django-registration, django-visits, etc.

Also, if you're working on a straightforward application like a news site, e-store, or blog, and you want there to always be a single, obvious way to do things.

Fostering team spirit: Django or Flask?

Although both frameworks are easy to maintain, Django is known for having a much more organized default layout than Flask - making it easier to adopt a new team member. Flask is a bit more flexible when it comes to file placement, so developers might waste some time simply exploring the libraries, extensions, and formats. 

The standards for Flask development have to be reconfigured by the team each time a project starts, so it's up to you whether or not you follow best practices. Django is less elastic in this regard and is a perfect solution for long-term projects with hundreds of thousands of lines of code.

Flask vs Django: Same...but different?

At the end of the day, both Django and Flask are capable of creating the same content and are just as customizable and high quality as the other. It comes down to what features you want and what is slowing you down.

For some, the Django framework might seem more challenging, due to the number of built-in batteries (thus, steeper learning curve). But once you've set up your first web app in it, you'll be able to develop the next one in no time. It's a solid option for those who need a fully functional, opinionated, and battery-powered framework without too much legwork to explore different ways of doing things.  So, if you want to use best practices to build fairly complex websites faster, you should choose Django.

And if you want the freedom to choose which tools you use and when to get creative with Flask. This framework covers only the basics of programming - passing incoming connections to a function and then returning the result as a web page. So if you want to go your own way without being guided by best practices, and have control over what components you want to use to squeeze out that last ounce of efficiency - this is a great place to start.

But don't be fooled, this ride is not for beginners. Especially not for those who have no idea about system design.

The best choice is to try both, release your application and see what suits you best. Both frameworks can do what you want. So, which one would you try first? 

Rated: 5.0 / 1 opinions
avatar male f667854eaa

Piotr Wawryka

Piotr has over 5 years of commercial experience writing Python applications. He is a software developer and data scientist at Codete since 2017 and a Ph.D. student at AGH University of Science Technology. His main field of interest is Neural Networks and their practical applications. He gives speeches at meetups and international conferences.

Our mission is to accelerate your growth through technology

Contact us

Codete Global
Spółka z ograniczoną odpowiedzialnością

Na Zjeździe 11
30-527 Kraków

NIP (VAT-ID): PL6762460401
REGON: 122745429
KRS: 0000983688

Get in Touch
  • icon facebook
  • icon linkedin
  • icon instagram
  • icon youtube
Offices
  • Kraków

    Na Zjeździe 11
    30-527 Kraków
    Poland

  • Lublin

    Wojciechowska 7E
    20-704 Lublin
    Poland

  • Berlin

    Bouchéstraße 12
    12435 Berlin
    Germany