DEV Community

Michael Learns
Michael Learns

Posted on • Updated on

Flask vs. Django?

So I was going to do a Flask tutorial this week after learning it in a week, but I decided to do a comparison with Flask and Django since I've had hands-on-experience with both python web frameworks. The reason is so that I can help you choose which framework to learn as learning a framework can be very time consuming —- but very fun of course!

CAUTION: I only spent a week with Flask whereas I've used Django for the past 3 years. Take this with a grain of salt 😅

Similarities

Let's start with similarities.

1) Flask and Django are both python web frameworks.

They enable you to create web applications using python for backend logic and html and css as front, or whatever front-end framework you'd prefer.

2) Can use ORM or object-relational-mapping.

This basically means, you can use SQLITE, MYSQL, PostgreSQL, and other databases that utilizes ORM.

-Django by default uses ORM.
-Flask does not come with ORM capabilities by default. You need to manually add it if you want.

3) Templating.

-Django uses a template engine similar to Jinja (actually Jinja was inspired by Django's own templating system)

Small comment: Django doesn’t use Jinja, but it’s own templating engine. They look similar because, as Jinja’s website says, “It is inspired by Django's templating system.”

-Flask uses Jinja as its template engine.

Jinja is basically like python for html (python + html = Jinja) that enables you to add logic to your html such us if/else, for-loops, and extension of templates which I really really love!

There are many more similarities between Flask and Django, but for me those are three the main ones.

Differences

The differences are quite big when you put Flask and Django side by side.

With Flask you get incredible flexibility.

You create the app.py (which is like the main logic for your app), the models.py (which is where you store your models that creates the tables for your database), the templating and etc. You have ABSOLUTE control on how you want to build the structure for you app.

With Django, it's all built for you!

The settings.py (which is like the config file for your app) the models.py, the urls.py (which is where all the routings are stored), the templating, etc.

Which is why Django's tag is, "The web framework for perfectionist with a deadline." Because everything is built in, you don't need to bother yourself with creating the files and thinking about how you should structure your app. It's all ready for you and you can immediately get started with building your app.

Conclusion

Flask and Django both have their strengths and weaknesses.
Each one has their own uses and purposes.

If you're making an app that's simple that doesn't anything too complex, then Flask is the way to go.

But if you're a beginner and want to get into web development with python, I'd suggest you start with Flask. The things you learn in Flask can be applied to Django. It's just easier with Flask.

Congratulations 🎉🎉! You made it! I hoped it helped you out!
Also, tell me if you'd like to see me do
a tutorial of Flask or Django on Youtube.
Thanks ya'll! Have a break, take a kitkat 🍫.

Thanks for all those who gave their feedback and added stuff to my knowledge on Flask and Django! Keep 'em coming ✌🏽

Follow me on Twitter and Instagram @heyimprax.
Subscribe to my channel in Coding Prax.

Top comments (36)

Collapse
 
fluffy profile image
fluffy

I'd look at things a bit differently: Django is a complete software stack for building an application, whereas Flask is some decomposed building blocks which allow you how to pick and choose how to build your application. Flask can absolutely be used for larger things just as well as Django, it's just that Django gives you more Stuff out of the gate. But you might not necessarily want to use the stuff Django provides, or want or need the baggage of having it loaded.

Like, you might not even need an ORM, or if you do you might want to use SQLAlchemy or ponyORM or Peewee, because each of them provides a different way of interacting with your data with different tradeoffs. With Django the most reasonable choice is the one-and-only Django ORM.

Same with things like authentication; in Flask you can pick and choose your authentication mechanisms (and choose whether or not to use Flask-Login for the session management, for example), you can choose to use any number of Markdown implementations (there's Flask-Markdown but I personally use Misaka because it works closer to how I need it to), you can choose whether or not to even have an admin interface (not all apps need one, after all), and so on.

For some examples, I built my own website in Flask (well, Publ, which is built on Flask), as well as flrig.beesbuzz.biz/ as a microservice sort of thing. I also used Flask to build a purpose-specific application server for a local apartment building's Internet-driven art installation; that basically only uses Flask as a very simple request routing system (with Jinja for the page rendering, since it's convenient).

Django definitely has its uses, but it's not necessarily that it's what you should use for a larger thing. It's very much a batteries-and-kitchen-sink included framework, and I'm not particularly fond of that sort of one-size-fits-all approach.

Collapse
 
imronlearning profile image
Michael Learns

Right! That makes sense as well! With Flask you kinda have it all in barebones and it's up to you as to what sort of blocks you want to add into it. While with Django it's all in ready for you.

So far with my one week experience with Flask, it's really all barebones. Completely flexible to what you want and need. It doesn't complicate you with the things you don't need but simply provides you with like a launchpad (figuratively speaking) to allow to make something immediately without all the complications.

With Django, it's all ready for you. The routing, the setup, the models, the templates, etc. It has it's own structure already that you don't need to bother with finding the things you may need for your project, because it's all already there.

For me personally, if you're just out there making something small I think that you should start off with Flask. Ofc, you can build something small with Django as well, but why overly complicate building your app by having to deal with Django's built in packages?

When I just started learning about web development I started out with Django and when I saw all these built-in packages that Django had, I had to learn what those do and how to deal with it. Which wasn't much of a pleasant learning experience.

The reason why I wanted to learn Flask was because I wanted to build something immediately, without making it perfect and having to deal with all the built stuff that Django has. If I only needed something, that's what I would get, and that's what Flask has enabled me to do.

Collapse
 
jaakidup profile image
Jaaki

Maybe you should first build some applications with these frameworks, and then only afterwards start thinking about writing about it.

Collapse
 
vkizza profile image
Vincent Kizza • Edited

Through Ben's article, many of us listening are greatly benefitting! In my view,it is a great way to get the experts out of their dens!

Collapse
 
imronlearning profile image
Michael Learns

Definitely!

Collapse
 
jeffguevin profile image
Jeff Guevin

The experts have already written countless guides to these frameworks, including head-to-head comparisons. Why not just find and read them?

Collapse
 
imronlearning profile image
Michael Learns

I actually did! Though, I'll admit I did only spend a week with Flask. I've spent more time with Django and I've been using it for the past 3 years now. 😅

Collapse
 
jaakidup profile image
Jaaki

I ran through some Django tutorials as part of my exploration of the python eco system. I like how Django code is built into "pods", but there is more of a learning curve as you need to figure out everything and the kitchen sink.

I was really surprises by Falcon, don't know if you've tried it.
I think it's very nice for pure API servers, no templates there, so it fits most of my needs and I felt totally comfortable with it after 30mins and a completed backend in an hour :)

I'm going to try Flask at some stage after going through ExpressJS this weekend.

Collapse
 
tobiassn profile image
Tobias SN

It seems to me that you have never used Flask before. It has absolutely no database functionality whatsoever, and thus no ORM.

Collapse
 
imronlearning profile image
Michael Learns • Edited

is that so? maybe i didn't state it properly. you can definitely create tables using the models.py sort of like similar to django's python manage.py makemigration and python manage.py migrate. except in Flask it's python manage.py db migrate and python manage.py db upgrade

Collapse
 
tobiassn profile image
Tobias SN

There’s no manage.py in Flask unless you created it manually. From the website: “By default, Flask does not include a database abstraction layer”

Thread Thread
 
rorixrebel profile image
Miguel Valdes

Agreed, seems like op has never truly used flask. ORM ain't part of it by default.

Thread Thread
 
imronlearning profile image
Michael Learns

Oh! Thanks 😄guess I missed that part.

Granted, I did only use and learn Flask within a week so I missed quite a bit of information. Thank you for making this clear 😅

Thread Thread
 
tobiassn profile image
Tobias SN

Glad I could help.

Collapse
 
jeffguevin profile image
Jeff Guevin

Ben, I'm glad you are engaging with these topics, and are willing to correct mistakes when they're pointed out. But do you really think you have the experience level to write this article at all? This is now publicly available, and frankly what you have here misses the mark as a discussion of Flask, Django, and any substantive comparison of the two. I can imagine other inexperienced developers reading this and making poor platform decisions based on what you've presented. I'd like to suggest you publish in areas you have expertise in, rather than angling for easy clicks on topics where you're not really ready to contribute.

Collapse
 
joeizang profile image
Joe Izang

Hi Ben,
Every python developer knows that sqlalchemy is powerful. So you are not entirely correct by saying Django orm is more powerful than flask-sqlalchemy. Anything you can do with sqlalchemy you can with flask-sqlalchemy. Once upon a time Django orm couldn't do many to many without some hacks but you could with flask-sqlalchemy. Also, flask can do a lot more than you think, so mini apps isn't entirely accurate. Flask puts more power in the developers hands because it's configurable while Django thrives on conventions.
With these frameworks, it's just a case of picking your poison. But looking at the facts, you are off to a great start.

Collapse
 
imronlearning profile image
Michael Learns

Woah really? Thanks Joe! I haven't really used sqlalchemy before and only really encountered it in flask-sqlalchemy.

Also my evaluation of Flask was only based on my one week experience with it along with what I've heard from fellow developers in my community. 😅

Perhaps in using Flask more I'd be able to learn more about it's capabilities and it's strengths.

Thanks man! I'll be sure to learn more about it. 🎉👍

Collapse
 
collins73 profile image
Demayne Collins • Edited

True about the databases not being included by default, but i still like to use flask to customize my databases using OOP. I think its easier than getting the batteries included approach with django..personal preference.

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

what do you mean by "Flask querying methods"?

Collapse
 
imronlearning profile image
Michael Learns

I think it should've been querying capabilities 😅by it I meant that Flask's querying capabilities are a bit limited compared to Django.

You can refer to this link for a list of Django's Queryset API's: docs.djangoproject.com/en/2.1/ref/...

And here for Flask: flask-sqlalchemy.pocoo.org/2.3/que...

As you can see Django has more options available for you compared to Flask.

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

no i mean, flask does not have in-built querying capabilities.

flask-sqlalchemy is just a wrapper for sqlalchemy, a normal python orm

Thread Thread
 
imronlearning profile image
Michael Learns

Oh! Sorry, I failed to mention that. 😅 Flask on its own does not come with querying capabilities. I recall having to install flask-sqlalchemy via pip in order to do querying from my database. Thanks! 👍

Thread Thread
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

i appreciate your article though, having written it, now you know more about the matter!

Thread Thread
 
imronlearning profile image
Michael Learns

Yes definitely! I've learned more about Flask and Django through it 😄 thanks for clarifying this too!

Collapse
 
blackbird profile image
Omkar Ajnadkar

I am working with both Django and Flask for more than 2 years...Although Flask is my go-to framework if I want to create any project or simple website to visualize my ML models due to its simplicity and customization(like I love to use MongoDB as my go-to database and remote availability in mlab), I agree with the fact that structuring become little bit problem if the project becomes bigger and bigger.
Said that, if you want to create something small quickly, I will suggest going for Flask. Like, when our semester results were declared recently, friends were finding it difficult to calculate overall CGPA of all semesters. So I was able to create this cgpa-calculator.herokuapp.com and host it on Heroku in just 30 mins.

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao • Edited

Most of the time flask is used to build REST APIs but Django can be used to build that as well due to the Django packages ecosystem like Django REST Framework.

If your a beginner I would suggest them to learn Django first due to battery built-in reasons and it allows one to focus on learning the 3 tier architecture that is useful to segregation of your code.

Collapse
 
miloszstx profile image
Miłosz Gałganek

This is a cool article, Ben. Maybe not the most in-depth (as some other commenters have pointed out), but I think it presents the most important points in a nutshell. Sometimes a short summary can be the most valuable. I notice you have mostly a developer's perspective on this subject; what about the perspective of someone who manages development teams? Someone like a Project Manager or CTO would probably make framework decisions based on different criteria, such as their budget and timeline. If anyone here would like to explore the "manager side" of this subject, here's one article I'd recommend: stxnext.com/blog/2019/05/31/flask-...

Collapse
 
programworld999 profile image
Pyfaraz

Just few days ago I started to learn Django. I been a PHP developer(code PHP).But now I am a big Django fan. I love this framework. I was thinking that I will learn Laravel. but I don't think that I have to learn that anymore. (But still I want to say that core PHP is so easy for me)

Collapse
 
thesohelshaikh profile image
Sohel Shaikh

What would be some good resources to start learning Django?

Collapse
 
imronlearning profile image
Michael Learns

I started learning about Django here:

tutorial.djangogirls.org/en/

I would highly recommend it 👍

Collapse
 
djmoch profile image
Daniel Moch

Small comment: Django doesn’t use Jinja, but it’s own templating engine. They look similar because, as Jinja’s website says, “It is inspired by Django's templating system.”

Collapse
 
thebjorn profile image
Bjorn

Django can definitely use Jinja as a templating language since 1.8 (docs.djangoproject.com/en/2.1/topi...)

Collapse
 
djmoch profile image
Daniel Moch

Not arguing otherwise. The point here is to compare default behaviors between Flask and Django. You can also use SQLAlchemy instead of Django’s built in ORM if you want.

Collapse
 
imronlearning profile image
Michael Learns

Oh! Gotcha, I'll add that in. Thanks! 👍

Collapse
 
nicolaerario profile image
Nicola Erario

Flask is simple and powerful but, when I realized that I’m trying to “structure” my project, I went to Django

Collapse
 
vkizza profile image
Vincent Kizza

Lots of thanks for the article. I would greatly appreciate a flask tut.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.