Flask vs Django

In this article, we will take a look at two of the most popular web frameworks in Python: Django and Flask.

Here, we will be covering how each of these frameworks compares when looking at their learning curves, how easy it is to get started. Next, we'll also be looking at how these two stand against each other with concluding by when to use one of them.

Getting Started

One of the easiest ways to compare two frameworks is by installing them and taking note how easily a user can get started with it, which is exactly what we will do next. We will try setting up Django and Flask on a Linux machine and create an app to see how easy (or difficult) the process is with each one.

Setting up Django

In this section, we will set up Django on a Linux-powered machine. Best way to get started with any Python framework is by using virtual environments. We will install it using pip.

$ sudo apt-get install python3-pip
$ pip3 install virtualenv
$ virtualenv --python=`which python3` ~/.virtualenvs/django_env

Note: If the pip3 command gives you an error, you may need to prefix it with sudo to make it work.

Once we're done setting up our virtual environment, which we've named django_env, we must activate it to start using it:

$ source ~/.virtualenvs/django_env/bin/activate

Once activated, we can finally install Django:

$ pip install Django

Suppose our project is called mysite. Make a new directory and enter it, run following commands:

$ mkdir mysite
$ cd mysite
$ django-admin startproject mysite

If you inspect the resulting project, your directory structure will be shown as:

mysite/
  manage.py 
  mysite/
    __init__.py 
    settings.py 
    urls.py 
    wsgi.py

Let's take a look at what is significant about each of the directories and files that were created.

  • The root mysite/ directory is the container directory for our project
  • manage.py is a command line tool that enables us to work with the project in different ways
  • mysite/ directory is the Python package of our project code
  • mysite/__init__.py is a file which informs Python that current directory should be considered a Python package
  • mysite/settings.py will contain the configuration properties for current project
  • mysite/urls.py is a Python file which contains the URL definitions for this project
  • mysite/wsgi.py acts as an entry for a WSGI web server that forwards requests to your project

From here, we can actually run the app using the manage.py tool. The following command does some system checks, checks for database migrations, and some other things before actually running your server:

$ python manage.py runserver

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

September 20, 2017 - 15:50:53
Django version 1.11, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Note: Running your server in this way is meant for development only, and not production environments.

To check out your app, head to http://localhost:8000/, where you should see a page saying "It worked!".

But wait, you're still not done! To actually create any pages/functionality in your site, you need to create an app within your project. But why do you need an app? In Django, apps are web applications that do something, which could be a blog, a forum, or a commenting system. The project is a collection of your apps, as well as configuration for the apps and entire website.

So, to create your app, move in to your project directory and run the following command:

$ cd mysite
$ python manage.py startapp myapp

This will create another directory structure where you can actually manage your models, views, etc.

manage.py
myapp/
  __init__.py
  admin.py
  apps.py
  migrations/
  models.py
  tests.py
  views.py
mysite/
  __init__.py 
  settings.py 
  urls.py 
  wsgi.py

From here, you need to set up your views in views.py and URL routing in urls.py, which we'll save for another tutorial.

But you get the point, right? It takes a few commands and quite a few files to get your Django project up and running.

Setting up Flask

Just like Django, we will use a virtual environment with Flask as well. So the commands for activating a virtual environment will remain the same as before. After that, instead of installing Django, we'll install Flask instead.

$ pip install Flask

Once the installation completes, we can start creating our Flask application. Now, unlike Django, Flask doesn't have a complicated directory structure. The structure of your Flask project is entirely up to you.

Borrowing an example from the Flask homepage, you can create a runnable Flask app from just a single file:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

And running the app is about as easy as setting it up:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

$ FLASK_APP=hello.py flask run
 * Running on http://localhost:5000/

Visiting the URL http://localhost:5000/ should display the text "Hello World!" in your browser.

I'd encourage you to look for some sample apps on the Flask homepage to learn more. Learning by example is one of the best ways to get up and running quickly.

The framework that "wins" this area is really up to your needs and experience. Django may be more favorable for beginners since it makes decisions for you (i.e. how to structure your app), whereas in Flask you need to handle this yourself.

On the other hand, Flask is simpler to get running since it requires very little to get going. An entire Flask app can be composed from a single file. The trade-offs really depend on what you need most.

Learning Curve

Regarding the learning curve, as we saw in the last section with Flask, it was very easy to get started. The app doesn't require a complicated directory structure where you need to remember which directory/file does what. Instead, you can add files and directories as you go according to your usage. This is what Flask is about, as a micro-framework for web development.

Django, on the other hand, has a bit higher learning curve since it is more "picky" about how things are set up and work. Because of this, you need to take more time learning how to compose modules and work within the confines of the framework.

This isn't all bad though, since this allows you to easily plug in 3rd party components in to your app without having to do any work integrating them.

Employability

Which of these frameworks will help you land a job? For many developers, this is one of the more important questions regarding certain libraries and frameworks: which will help me get hired?

Django has quite a few large companies on its resume, which is because many companies that use Python for web development tend to use (or at least started off with) Django to power their site. Django, being a full-fledged framework, is often used early on in development because you get much more resources and power with it out of the box.

Here are just a few companies that use (or have used) Django for their sites:

  • Pinterest
  • Instagram
  • Disqus
  • NASA

Flask is a bit harder to gauge here, mostly because of the way it is used. Flask tends to be used more for microservices, which makes it harder to tell which companies are using it. Plus, companies with a microservice architecture are less likely to say their service is "powered by Flask" since they likely have many services potentially using many different frameworks.

There are, however, hints out there of who uses Flask based on job postings, tech talks, blog posts, etc. From those, we know that the following companies have used Flask somewhere in their backend infrastructure:

  • Twilio
  • Linkedin
  • Pinterest
  • Uber
  • Mailgun

While Django may be more popular among companies, Flask is arguably more common among the more tech-focused companies as they are more likely to use microservices, and therefore micro-frameworks like Flask.

Project Size and Scope

Our comparison of each framework can become very subjective thanks to many different factors, like project scope, developer experience, type of project, etc. If the project is fairly small and it doesn't need all of the overhead that Django comes with, then Flask is the ideal choice to get started and get something done very quickly.

However, if the project is larger in duration and scope, then Django is likely the way to go as it already includes much of what you need. This basically means that many common components of a web-service/website either already come with Django, or it is already available through 3rd party open source software. In some cases you can just create a Django project, plug in a bunch of components, create your views/templates, and you're done.

While we do praise Django for its extensibility, we can't ignore that Flask does have some extensions of its own. While they aren't quite as big in scope as Django (and many of these extensions come standard in Django), it's a step in the right direction.

Django's add-on components can be as big as a blog add-on, to as small as small middleware input validation. Most of Flask's extensions are small middleware components, which is still better than nothing and very helpful, considering the average size of Flask projects.

Limitations

Every piece of tech has its problems, and these frameworks are no different. So before you choose which to use, you might want to know what disadvantages each has, which we'll be talking about in this section.

Django

So, what are the aspects of Django that work against it to be selected as your framework of choice?

Django is a very large project. Once a developer, especially beginners, starts learning Django, it's easy for them to get lost in the source code, the built-in features, and components it provides, without even using them in an app.

Django is a fairly large framework to deploy for simple use-cases, as it hides much of the control from you. If you want to use something that isn't "standard" in Django, then you have to put in some extra work to do so.

Understanding components in Django can be a little difficult and tricky at times and can lead to tough decisions, like deciding if an existing component will work for your use-case, or if it'll end up causing you more work than it is worth.

Flask

Now that we've seen some of the problems with Django, let's not forget about Flask. Since the Flask framework is so small, there isn't a lot to complain about. Well, except for that fact right there: It's so small.

Flask is a micro-framework, which means it only provides the bare-bones functionality to get you started. This doesn't mean it can't be powerful and can't scale, it just means that you'll have to create much of the functionality of your service yourself. This means you'll need to handle integrating your database, data validation, file serving, etc.

While this could be considered an advantage to those who want control over everything, it also means it'll take you longer to get set up with a fully-functional website.

Choosing Flask or Django

While it's easy to talk about what each framework does and doesn't do, let's try and make a more direct comparison of each, which we'll do in this section.

When simplicity is a factor, Flask is the way to go. It allows much more control over your app and let's you decide how you want to implement things in a project. In contrast to this, Django provides a more inclusive experience, such as providing a default admin panel for your data, an ORM on top of your database, and protection against things like SQL injection, cross-site scripting, CSRF, etc.

If you put a lot of emphasis on community support, then Django is probably better in this regard given its history. It has been around since 2005, whereas Flask was created in 2010. At the time of writing this article, Django has about 3.5x more questions/answers on Stack Overflow than Flask (about 2600 Django questions to Flask's 750).

The Flask framework is relatively lightweight. In fact, it's almost 2.5x smaller than Django in terms of the amount of code. That's a big difference, especially if you need to understand the inner-workings of your web framework. In this aspect, Flask will be much easier to read and understand for most developers.

Flask should be selected for development if you need complete control over your app, which ORM you want to use, which database you need to integrate with excellent opportunities to learn more about web services. Django, on the other hand, is better when there is a more clear path to creating what you want, or you're creating something that has been done before. For example, a blog would be a good use-case for Django.

Learn More

Want to learn more about either of these frameworks? You can get a great start by visiting each framework's respective websites:

Conclusion

In this article, we compared the two web frameworks, Django and Flask, by looking at their different properties and setting up a simple "Hello World!" app with each one.

You may find that if you're new to web development and decide to learn Django, it may take you a bit longer to truly understand what all of the underlying components do, and how to change them to actually do what you want. But there are many positives as well, and once you become proficient with Django, it'll end up saving you a lot of time in the end, given its huge list of components and vast community support.

A more advanced comparison for any frameworks can be done only with advanced use cases and scenarios. Just know that you can't really go wrong with either one, and learning either will set you up well for finding a job.

If you need a recommendation, then I'd personally go with Flask. By learning a framework that doesn't hide so much stuff from you, you can learn much, much more. Once you have a better understanding of the core concepts of web development and HTTP, you can start to use add-ons that abstract this away from you. But having that solid foundation of understanding is more important early on, in my opinion.

Which framework do you use, and why? Let us know in the comments!

Last Updated: July 8th, 2024
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms