Flask vs. Django: Which Web Framework is Best?
Flask vs. Django
Web frameworks provide a standard set of tools that enable developers to build things fast.
Flask and Django are by far the two most popular open-source Python web frameworks.
Both are excellent. But knowing which one to choose for your project — or deciding whether to incorporate both — can be a daunting task.
In this article, we’ll explain the differences between Django vs Flask to help you choose the right one for the job.
Last Updated September 2022
Finally create that App & fully-functioning user database in this crash course to building a REST API | By Mark Winterbottom, Brooke RutherfordExplore Course
But first, what is the purpose of a web framework?
We cover this at length in What is Django?
To recap, web frameworks provide the basic functionality needed to get a website up and running. They handle things like HTTP requests, returning HTML documents, and responding to requests. (Things that go on behind the scenes every time a user visits a website.)
Web frameworks also help to organize code by providing structure.
For instance, creating a website involves mapping URLs to code. Frameworks provide a predetermined structure for doing this.
Each framework provides varying degrees of features and decision-making. Some are more opinionated, like Django, which means many decisions are not up to you, saving you time and decision fatigue.
Flask, on the other hand, is a minimalist web framework. It demands more decision-making from you but offers you flexibility and control over your application development in return.
Django is a web framework for Python that comes fully-loaded. It provides everything you need to get started.
Design decisions — like where to put code to handle specific tasks and how to configure your app — are made by Django. This saves a lot of time because you don’t need to install this functionality yourself.
Here are some features Django provides the moment you install it:
- Built-in ORM (object-relational mapper) for mapping objects to your database
- Migrations for managing changes to your database
- Admin interface for managing your application’s data through a web interface
- Form validation for checking users’ input is valid
- Settings configurations for customizing your application once it deploys
- A development server for testing your code as you develop it
- A template engine for generating HTML pages from code
- Sessions for storing data about users as they navigate through your website
- Authentication services allowing users to create an account and log into the account
- Command line interface (CLI) that administrators can use to perform maintenance tasks such as exporting and importing data and database cleanup
- A unit testing framework for writing tests
On top of this, you can install Django REST Framework to boost the full-featured functionality already provided by Django. This empowers you to create RESTful APIs for your simple or complex web app.
But all these features come at a cost.
With Django, all the features listed above are in your project, regardless of whether you want them or not. This is handy for complex web apps. But for simple tasks, it may mean adding far more dead weight than necessary. This code will increase the size of your application, loading times, and complexity.
If you’re building a very small website — like a single webpage for your professional bio — Django may be overkill. It’s unlikely you will use 99% of the features it comes with.
So what then?
Flask is a micro-framework. It has no built-in authentication system, admin site, or fancy Object-Relational Mapper. It maps URLs to code, handles requests, and returns HTML templates as responses, and that’s about it.
With Flask, you get to make the decisions.
Need database integration? Build it yourself. Or choose one of the external libraries that provide this functionality, like SQLAlchemy.
With Flask, if you want to do anything more than handle HTTP requests and render web pages, you have two options:
- Find a package that does what you need.
- Build the functionality yourself.
This minimalist approach applies to how you structure your code, as well.
Unlike Django, Flask doesn’t have strong opinions on where you should put your code. You are free to organize your application as you (or your team) see fit.
This is great if you have an idea of how you want to structure your project. Or if your project is so small, there won’t be much code to structure anyway.
But in any other situation, these decisions may be very time-consuming. And your time is better spent writing code to create the unique value your project intends to offer.
Flask vs. Django: Which framework is best?
Ultimately, it depends. Let’s look at the different types of applications to determine which framework would be best for each situation.
The term “microservice” describes an application that performs a very specific task.
Microservices are smaller pieces of a larger puzzle that work together to create an app.
Take the streaming service Netflix. Their architecture consists of 700+ microservices, each responsible for their own specific task. One microservice retrieves video metadata, another displays lists of movies. One manages customer information, and so on.
700+ microservices sounds like a lot. But there are many benefits to structuring your architecture in this way.
It provides the option to scale parts of the application from others.
Let’s say your “video metadata” service is getting a lot of traffic. To prevent it from crashing, you can divert resources from less busy microservices (like a list of a user’s favorite films) to higher traffic services, like the homepage.
With this type of architecture, you can mix and match programming languages and frameworks. It’s not uncommon to find a mixture of Django and Flask in a microservice architecture like Netflix. Because it is small and lightweight, Flask is perfect for creating such services.
The term REST refers to Representational State Transfer. API means Application Programming Interface. Put together, REST API describes an application that helps transfer data between applications.
Almost every single application or interactive website you use today runs using a REST API.
When you post a tweet via the Twitter app, a REST API is behind the scenes, sending the tweet to the Twitter servers for others to read.
This is the case for any application you use that sends data to a server.
Creating a REST API is like building a website. Both use HTTP (HyperText Transfer Protocol) to display content. The main difference is that a REST API is for another app to use, whereas websites are for humans.
Either way, the underlying technology is the same. So if you want to build a REST API with Python, you will need to use a framework. The Django REST Framework (DRF) is an extremely powerful tool.
DRF bolts onto Django to provide a large selection of time-saving features to build REST APIs.
You get a browsable API right out-of-the-box. This saves time when building the API. It also saves time for the developers integrating your API because they can view and test endpoints in the browser.
Another perk is automated documentation.
This is crucial because good quality documentation is how developers learn to use your API.
This helps them to better understand your API and speeds up the process of integrating it with their tool. Your API may have the best features in the world, but without documentation, no one will know how to use it. It also saves you lots of time writing documentation, so you can focus on creating a great API.
So what about your standard, run-of-the-mill website?
With the launch of drag-and-drop websites and blogs, many people assume a website is a simple project.
There may be similarities on the surface, but behind every website is an array of complex logic and decision-making.
Determining the right framework to use depends on the type of website you plan to build.
Small Traditional Websites
This kind of website consists of a handful of pages and very few interactive features. It may be suitable for a portfolio or professional profile.
For this task, you will likely need to focus on the HTML of your application.
If you are building something like this in Python, Flask is better.
Interactive Traditional Websites
Many websites are what I call “interactive traditional websites.”
They are traditional because they use standard HTML templates. But interactive because they use authentication and data to power the user experience.
This could be your favorite news site, message board, or even social media site.
These websites enjoy the huge time savings offered by leveraging Django’s built-in features.
In most cases, you will need to integrate with a SQL database such as PostgreSQL, MySQL, or MariaDB. The Django ORM supports all of those mentioned.
You also get the Django admin feature, which means you don’t need to spend time building boilerplate code to manage your database items.
Progressive Web App
These websites are applications that run in the browser. Think of services like Trello, Google Docs, or Google Keep.
These sites offer an interactive experience with very minimal page reloads or refreshes. In some cases, they work offline, like Google Docs.
Often these websites consist of two key components:
- REST API
As mentioned, the DRF combines with Django to add features for building high-quality REST APIs. Thus, Django is usually best for these types of projects.
One exception might be if you need a very small REST API to handle your app’s functionality — 2-3 APIs for performing basic functionality like triggering emails.
In this case, Flask may be better. You can benefit from the lightweight and minimalist nature of the framework.
When building these websites, you’ll need a combination of REST APIs and static page rendering.
Using Django with the Django REST Framework provides the best of both worlds.
However, if your website is a large platform, you may want to consider the microservices architecture mentioned above.
So what should you learn?
Both Flask and Django are excellent frameworks, and being proficient in both will benefit your career as a Python developer.
But if you’re thinking of learning one with a specific project in mind, take a look at the table below to help guide you in the right direction:
|Handle HTTP Requests||Yes||Yes|
|Built-in auth system||No*||Yes|
|Built-in database integration||No*||Yes|
|Built-in admin site||No*||Yes|
|Built-in CLI tools||No*||Yes|
|Built-in form validation||No*||Yes|
|Great for microservices||Yes||No|
|Good for rapid development of popular website features (database integration, admin site, auth)||No||Yes|
* Possible to do, but you need to build it yourself or find (and learn) an external library.
In summary, if you want to make use of the built-in Django features, go with Django. If you don’t think you will use these features, and/or you prefer to choose your own packages or build functionality from scratch, go with Flask.
If you want to continue your search for the perfect web framework, let’s see how Node.JS compares!
Top courses in Django
Django students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.