Both Flask and Django are:
- Web frameworks
- Coded on python
- Opensource products with great communities
- Both could be used for Server Side Rendering (for SEO purposes) or building REST APIs (e.g. JSON) for SPAs
- Both have blocking programming model which require multi-thread/multi-process runners like gunicorn to deploy
What is the difference Flask vs Django? TL;DR:
- Django is an all-in-one solution: Relational DB ORM + Migrations, Template rendering system, Security middleware, User management built-in application, configurable CRUD admin panel, and it is all in official distribution
- Flask is microframework - you have to install and configure all packages for required features by yourself, configure the project and automotive pipelines, you will spend much more time on doing it for each new project
Django Internal ORM
1. Define the model in
models.py with e.g.
class Person(models.Model): name = models.CharField(max_length=30) age = models.IntegerField()
python manage.py makemigrations
python manage.py migrate
That is all! It will generate
CREATE TABLE SQL statement and execute it under the hood to synchronize your models to database.
Then just do:
p = Person(name="Alex Kotli", age=28) p.save()
And Djangos ORM will perform
INSERT SQL query under the hood. If you will write:
And it will do
SELECT SQL which will return return an iterable with all persons with age grater then 18.
Flask vs Django ORM
Flask doesn't has ORM.
pip install side-vendor ORM called
SQLAlchemey, and then connect it by imports/flask-plugins. But SQLA itself does not has a migrations, so you will then need an Alembic for migrations, and then you will need to configure both (several files but it takes time anyway)
Django admin area
Create boilerplate app, run
python manage.py createsuperuser and then log in to
It already allows us to manage users/roles/permissions and could be customized for any new model! Flask doesn't has it.
What to choose in flask vs django?
Make your choice by considering one by one all pros and cones.
Django benefits, Flask disadvantages
- Django will give you an all-in-one solution which will give you everything needed to build great web apps, on Flask you will have to do a lot of extra jobs to gather all things together
- Django is much faster in terms of development time, but only when you have experience working with it. Flask is faster if you never did web programming
- Django will cover you from security attacks if you follow recommendations and not doing stupid things that reduce security level, like removing CSRF protection instead of adding couple of lines of code to satisfy it, allowing cors to *, disabling hostname check. On Flask you will have to be aware very wel about the most common web vulnerabilities and will have to find packages to implement it by yourself. And then perform security audit. Flask has no internal security level at all.
- Most of Django libraries (e.g. OAuth sign up) are much easier in use, have more features, and are up-to-date
- Django implements a secure login system for you
Flask pros, Django cones
- You have to learn Django longer when flask ready to serve http requests after pip/pipenv install
- With Flask you will use more external libraries which could be then used in other e.g. non-web python apps - free experience
- More lightweight apps, but it will not give you a performance boost (you will save only a couple of CPU loops), most issues come with heavy and joined DB requests without indexes which could happen on both frameworks when you don't analyze the performance
- You are flexible to implement some different methods e.g. Django uses sessions over coockie when you can craft your own JWT way to handle Authorisation, again with a huge risk of security attaks if you do a small mistake