Flask vs. Django: How to Choose Your Python Framework

By: Justina Hwang

Category: Blog

In this post, we’re going to compare the most popular frameworks Python developers use as part of their technology stack — Flask vs. Django — and help you decide between them. The short answer? If you’re a relative beginner to Python or to coding in general, Django is a great place to start, since it requires less manual set-up. If you’d like a less bulky, more flexible Python framework and don’t mind doing more to set it all up, Flask could be a great choice.

That said, if you’re new to web development, terms like “framework” or “technology stack” might sound like another language — and it’s absolutely okay to be a little intimidated! So let’s quickly define terms before we get into the details. Think of a framework as a set of guidelines that help you write your code and build your application, and a tech stack as a group of technologies you layer together to build products (e.g. your front end framework, your database application layer, your back end server, etc.).

Table of Contents

  1. What to know about Python frameworks and your Python technology stack
  2. How to choose a framework: Flask vs. Django
  3. Pros and cons of Flask
  4. Pros and cons of Django
  5. Python tools for web development and data science
woman in glasses with hand on face, thinking

Is Tech Right For you? Take Our 3-Minute Quiz!

You Will Learn: If a career in tech is right for you What tech careers fit your strengths What skills you need to reach your goals

Take The Quiz!

What is a Python framework, and what does it have to do with a technology stack?

You can read more here all about what Python is, how to learn Python and what you can do with Python 🐍. But let’s take a closer look here at what we mean when we talk about Python frameworks.

To break it down with an analogy, we can compare web development to the American education system. When you design a web application, it is very useful to follow certain guidelines. This is similar to giving teachers curriculum standards to know what they need to cover in a given school year. Without these guidelines, third grade teachers might not know whether to teach subtraction or division first.

With an educational framework, everything has a time and place that helps teachers properly instruct their students at an age-appropriate level. Similarly, having guidelines in web development helps you know where to put your code, establishes how the code is processed, and provides a structure for the developer to follow. Like educational guidelines, a framework is not a tool as much as a way of organizing your code and executing it.

To use a framework like Django or Flask with Python, you install the framework on your computer using pip-install — with Terminal on Mac or Command Prompt on PC — in much the same way as you’d get set up to write in Ruby on Rails.

If a framework is like a curriculum or learning system, then we could say a tech stack, on the other hand, is the set of tools you use to make it all happen. In our comparison, this is like using the English language for classroom teaching. In the United States, we use 8.5 x 11 paper instead of the A4 paper used in Europe and Asia, round bubble scantrons for tests, and three-ring binders. Similarly, tech stacks are the tools you need in your development environment to do things like communicate with servers and store data.

Fun fact! The term “stack” is used in other contexts too. For example, a “marketing stack” includes all the various tools you choose from to accomplish marketing tasks and measure outcomes — like storing contact information, sending emails, posting blogs, etc.

Let’s look at a sample Python stack:

  • Programming language: Python
  • Database for storing data long-term: MySQL
  • Web server receiving requests from clients and passing it to the application server: Apache
  • Framework for doing back end development: Django or Flask

Django and Flask are two well-known frameworks commonly used in Python tech stacks. The Django framework has been a developer favorite, and Flask has been gaining popularity. While both Flask and Django can both help you deploy web applications and data science projects, they are two different means to an end.

📌 Related: What is Python?

(Back to top.)

How to choose a framework: Flask vs. Django

Not sure how to choose your framework? Let’s take a closer look at how Flask and Django operate.

Flask is a microframework — a framework that doesn’t come with tools or libraries. It’s missing conveniences that a full framework has. For example, Flask doesn’t come with data abstraction layers, meaning that if you have the need to store data long-term and you want to save the data in a database such as MySQL, SQLite, or PostGreSQL, you have to write a connector yourself — and write pure SQL code to read or write to the database.

Flask code defining REST endpoints

This is what Flask looks like when it’s defining REST endpoints — /api/actors and /api/actors/add are URLs this app will respond to.

Django is an open source web framework that uses what the Django Foundation calls a Model-Template-View (MTV) pattern. An MTV pattern gives you a design pattern and a template for best practices that is convenient and easy to modify. If you’re familiar with Ruby, it might help to know that MTV loosely resembles the Model-View-Controller (MVC) pattern used by Ruby on Rails, which makes it easier for beginners to transmit data to the server and navigate stored data than Flask.

Django code showing the five latest poll questions in the system

Using Django to write a view that displays the five latest poll questions in the system.

Check out Django’s documentation and Flask’s documentation to learn more.

So how do you choose between Flask vs. Django?

Which framework is a better fit for you depends on your working style. You can use either Django or Flask to build web applications and interact with the data science components in your web applications — so, depending on the project, both are good options as frameworks within the Python technology stack.

If your goal is to make a minimum viable product, Django’s robust MTV system can help you deploy quickly in web development. If you’re interested in data science and don’t require the features of a full web framework in order to analyze and manipulate your data, then Flask might be a better fit for you.

Let’s take a closer look.

(Back to top.)

Pros and cons of Flask

Flask pros

If you’re more of a DIY type that would prefer to customize your framework, then you might be interested in trying Flask. Flask allows you to use any kind of extensions you want and is more lightweight than Django. While Django has built in tools and libraries, you can decide what you want to use with Flask.

If Django is like a set meal, Flask is more like ordering a la carte. Instead of being presented with a readily determined appetizer, main course, and dessert, Flask allows you to pick what you want for each part of the meal. It’s comparable to building your own system in Linux versus buying a pre-installed Windows or Mac operating system.

Flask comes with a debugger, HTTP request handling, support for ORM plugins, and support for cookies. It has multiple modules that make it easier for a web developer to write applications without having to worry about the details like protocol management and thread management — important basic communication protocols.

Protocol management also means you don’t have to worry about how to structure your HTTP requests. With Flask, you don’t have to worry about property formatting your HTTP and HTTPS responses. Thread management means that when multiple users come to your server at the same time, you don’t need to stress about one request somehow interacting with another request.

Flask cons

If you have a simple project, a microframework might be appropriate because it’s fast and lightweight — but if you have a much more complex project, Flask might not have enough tools to help you complete the project conveniently and quickly. You’d either have to write things yourself or add other tools along the way. It’s like a RaspberryPi computer in the sense that it has all of the basic features you need, but it’s not as convenient as having a fully built PC that has way more features and functionality right out of the box.

So one con of Flask may be having to DIY if you’re new to programming. Designing and installing the different components you want in your Flask framework means investing a lot of time upfront to do your research. If you’re a beginner or you’re not interested in manually configuring your framework, the time-sink may not be worth it for you.

📌 Related: How To Learn Python (A Complete Guide)

Dev.to brings up two other Flask cons, which are scalability and security. They point out that because Flask has a singular source, it means that every request is handled in turn, one at a time. This means that if you are trying to serve multiple requests, it will take more time — and with fewer tools at your disposal, you may need to install more modules.

Installing more modules, however, can mean that it’s less secure, because you’re using third-party modules. This presents greater risk. “The process and development is no longer between the web framework and the developer, because of the involvement of other modules. That could increase the security risk if a malicious module is included.”

(Back to top.)

woman in glasses with hand on face, thinking

Is Tech Right For you? Take Our 3-Minute Quiz!

You Will Learn: If a career in tech is right for you What tech careers fit your strengths What skills you need to reach your goals

Take The Quiz!

Pros and cons of Django

Django pros

Django is great for beginners to web development and those learning Python. The MTV design pattern offers an easy way to onboard newer developers because all of the components that you need to build a website can be found in one place.

If you’re trying to update content, style your page, and define the logic in your code, those are all found in distinct folders within the framework, making it convenient to find the files you need and making it easy to conceptualize web development.

This shows the way Django files are structured

Basic Django file structure.

Because Django is a full stack framework, it has built-in features and libraries to make things convenient. Django uses its own object-relational mapper to allow code to work across different databases and make migration easier from one database to another, which is great if your projects expand in size over time.

For the more intermediate and advanced Python developers, Django offers convenience and quality of life features as well. Django allows you to define URL patterns and has an object-oriented programming language database as well as object-relational mapping capabilities.

Django also has a cache framework that goes with multiple cache mechanisms, a built-in authentication system, and an automatic administrative interface feature that allows you to customize your editing and deletion capabilities.

Django cons

One con Django has is that it is extremely opinionated about the way things are done. As our Skillcrush lead development instructor Ann Cascarano says, “it’s basically the Django way or the highway — whereas with Flask there is a lot more flexibility.” But that’s not always a con. “Conversely,” says Ann, “the opinionated bit helps keep everyone on the same page and enforces rules and process.”

HackerNoon finds that other Django cons include being too monolithic, components being deployed together, and the fact that developers need knowledge of the full system to be able to make it work properly. If something is “monolithic,” it means as the application becomes more complex, the model file can get very large.

Because all of the code lives within the same files — all model-related code is in the model file, all view-related code is in the view file, etc. — it can be difficult working with large files. The downside to components being deployed together is you can’t update them individually, meaning it takes longer to update the code you want.

(Back to top.)

Python tools for web development and data science

In addition to frameworks like Django and Flask, there are Python tools and libraries that will make life easier for you as a web developer or a data scientist. Below are some tools to experiment with as you delve deeper into Python programming and get comfortable with your tech stack.

Python web development tools and libraries

If you’re interested in web development, SQLAlchemy may be a useful tool to explore further. SQLAlchemy is a “Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL” and provides a “full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.”

Translation by Ann: “SQLAlchemy is an open-source SQL toolkit with special functions that make it easy for you to set up your database and perform all kinds of database actions, without having to write raw SQL queries! (Trust me, this is a win.) Create your database, write to it, fetch from it — SQLAlchemy has got your back!” 😉

SQLAlchemy code for a database model

This SQLAlchemy database model will store data for a fitness tracking app.

Selenium is an open source automation framework for web applications that allows you to write test scripts in many other programming languages, including: Java, C#, Python, PHP, Perl, Ruby, and .Net. With Selenium, you can perform tests from any browser, including: Chrome, Firefox, Safari, Opera, and Internet Explorer on Windows, macOS, and Linux. You can also integrate Selenium with tools like JUnit and TestNG to manage test cases and generate reports.

Code from a Selenium test

Selenium testing in action.

Python data science libraries and tools

Not to be confused with the black and white bears that eat bamboo, pandas is an open source data analysis and data manipulation tool used in data science that offers data structures. Pandas will help you to explore, clean, and process your data. The pandas website offers a 10-minute tutorial on pandas for beginners.

Getting ready to use pandas to aggregate data

Let’s use pandas to aggregate our data.

NumPy, which stands for Numerical Python, is a library for Python that “provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.”

Translation by Ann: “NumPy is a data analysis library that helps you efficiently work with large datasets. NumPy is full of functions that are custom-built for handling complex data. NumPy has a variety of functions that allow you to inspect a dataset, perform calculations, and even combine datasets together!” 😀

If you’re interested in learning more about NumPy, there’s a vibrant community with several tutorials on how to use NumPy effectively.

Code to build a scatterplot using NumPy

Using NumPy to build a scatter plot with your data.

Psst 👀 If you’re excited to learn how to code after reading about the different frameworks and tools you can use to deploy web applications and manipulate data, check out our Break Into Tech program, which teaches you everything you need to know to be job-ready as a developer.

(Back to top.)

woman in glasses with hand on face, thinking

Is Tech Right For you? Take Our 3-Minute Quiz!

You Will Learn: If a career in tech is right for you What tech careers fit your strengths What skills you need to reach your goals

Take The Quiz!

Justina Hwang

Justina is a Content Manager at Skillcrush. When she's not learning how to code or having deep conversations about life with her cat, she can be found dreaming up travel adventures on a comfortable sofa somewhere around the world.