DEV Community

loading...

Django vs Flask: which one should you choose?

kaozdl profile image Kalil de Lima ・3 min read

Django vs Flask: which one should you choose for your project?

Django and Flask are among the most popular Python web frameworks.
In this article we're going to review their main strengths and weaknesses to help you decide which one is the better fit for your project.

Django

django-logo

Approach

Django opts for an all batteries included approach.
From a powerful ORM to an authentication system or a templating engine, Django takes care of it all without the need to learn any extra libraries.

It also has the concept of pluggable apps, that means that you can drop an app folder inside your project and get its functionality right away
with very little configuration.

Main features

  • Django has a very powerful ORM that exposes a lot of SQL to the python programming language.
  • Authentication and Authorization built in, without any extra configuration.
  • A built in admin site.
  • A built in mailing system.

Pitfalls

  • Tightly coupled with its toolset, Django is sort of difficult to use in combination with other tools (replacing the form library for example).
  • The default scaffolding, while not bad for small projects, does not scale without modification when trying to manage things such as multiple environments.
  • It implements the Model View Template pattern, and requires some effort to implement other architecture types with it.

Flask

flask logo

Approach

Born as a combination of an http server with a template engine zipped together, Flask opts for a very minimalistic approach.
It provides some minimal structure, but leaves the decisions up to you.
This makes flask very easy to extend with practically whatever you want.

Main features

  • Very lightweight.
  • It requires little to no configuration.
  • Not opinionated, you have total freedom to decide the project structure, from a single file to an elaborate folder system.
  • When you need some structure, you have blueprints to structure your app.
  • Easy to integrate with other python libraries.

Pitfalls

  • Since it's very lightweight, while the framework itself doesn't need any special configuration, the used libraries often do require config.
  • You have to mix and match libraries to get the desired functionality. This also means that different parts of your project have different support from the community.
  • The lack of opinion can lead to complicated architectures, or to very big single file projects.

Wrapping up

Django is a good idea for…

  • Quickly validating business ideas.
  • Ecommerce sites with standard features.
  • Information systems in general where reporting is important.

Flask is a good idea for…

  • Lightweight applications.
  • Microservices.
  • Non MVT or MVC projects.

So... Which one should you choose?

To properly answer this question you need to take into consideration all the project requirements and make a decision based on that.
If development speed is the most important thing because you need to go to the market fast, then Django probably will be the better choice,
since you get a more complete feature set out of the box.
If you need a more custom solution, or your project features are out of the ordinary Flask might be the better choice.
At the end of the day, you should choose the framework that adjusts more into your project idea and particular situation,
taking into consideration their strengths and weaknesess.

Do you have any experience that you want to share with any of these frameworks?
Are there any extra pros or cons that you feel are missing?
If you do I invite you to discuss them in the comment section.

Thanks for reading.

Discussion (3)

pic
Editor guide
Collapse
bigj1m profile image
Jean-Michel Plourde

I work on Django projects for the job and have done Flask projects for my classes. Flask is really great for smaller projects, you can get up and running in no time while Django would be an overkill solution and is more geared toward big projects that require lots of robust tooling.

I worked on big projects in both Django and Flask and I have to say to this day, I still haven't worked on a big project where Flask is better suited. Requiring multiple different libraries with different level of supports and documentation makes it sometimes painful to work with Flask. Just an example, you want to do forms: in Flask the most popular lib is Wtforms. It's great and it does the job. But it has some limitations, for example when you want to customize ChoiceField or MultipleChoiceField, you can't do a lot. Yes with Django you have to stick with the tooling, but it is highly efficient, customizable and extensible.

Another thing I don't particularly like in Flask is how you declare your links for your views. While adding decorators to you view functions is visually intuitive especially when you have parameters, I don't like how at some point if you have a lot of views divided in apps you get scattered. Yes there is blueprints, but you just put stuff somewhere else to register and you still have decorators scattered over many functions. I like that in Django you have your urls.py by app, you put the path and a reference of your view.

Great write-up btw, this is very informative for someone that can't decide between the two.

Collapse
ssglaser profile image
ssglaser

This write-up is great! At Oso (osohq.com/) we're working on making authorization better in Python apps. We have integrations for Flask, Django and SQLAlchemy... You may find them useful, check them out!

docs.osohq.com/python/reference/fr...

Collapse
sudarshansb143 profile image
Sudarshan Sawandkar

TL:DR
If you are creating instagram-2 then use Django or if you want WhatsApp-2 use Flask