Photo by Glenn Carstens-Peters on Unsplash

Flask vs Django

Jonathan Reeves
8 min readJun 24, 2021

Hello, today I want to talk about the differences between Flask and Django. I can honestly say that I have worked with both extensively and I love both of them. I am not against one or the other. I believe that both of these frameworks have their place in the web world and can be useful. It just depends on what your needs are.

Flask has been around for quite some time now. It is considered a microframework. What that means is that, unlike Django which is a full fledged web framework that comes with all the bells and whistles needed for web development, Flask only gives you a small framework to work with which allows you, the developer, to go about implementing features such as Auth yourself. You, the developer, are able to install the libraries and modules you need to complete your web application.

I want to get this out of the way now for anyone that may think I am bashing on Django. I love Django. If you haven’t had the opportunity to use Django then I highly recommend going out and finding a good tutorial or book about the framework and read through creating a couple of websites using Django. You will find that Django makes it fairly simple to get a backend up and running. As a matter of fact at the bottom of this page will be links to some really great free resources for learning Django. I highly recommend going through them as it will give you an idea of what Django is capable of and why some developers choose to use Flask over Django.

One of the added benefits of using Flask over Django is that nothing is abstracted away. Django does a lot of things under the hood. In a black magical box if you will. Django comes with many libraries and modules already preinstalled. For most people wanting to create a website such as a blog you have everything you need. It even has a nice built-in admin site that allows you to manage content easily without needing to hook up a CMS like Contentful or Shopify. Really great stuff. Here is an example of creating a hello world project with Django. Feel free to follow along so you can see the idea behind Django.

Django Hello World Example

To begin we will create a new directory for all of our code. Each new project will be in its own virtual enviornment. Python uses virtual environments so that you can have multiple projects using various versions of libraries and Python. It is a very handy feature. Because of how Python is installed on most systems you can overwrite a lot of your projects by upgrading to the latest version of Python which will typically break your older projects. So to eliminate this headache we create virtual environments. So follow along with the steps I am doing and we can get started.

  1. Open your terminal, cmd, powershell etc. Whichever one you are most comfortable with will work.
  2. Navigate to a directory that is easy for you to get to, if you are using a work computer I would recommend storing the code on your Desktop.
cd Desktop && mkdir flask_vs_django && cd flask_vs_django && mkdir hello_django

This will change your terminal to the desktop and then make a directory flask_vs_django and then change into that directory and create another directory called hello_django. We are using the hello_django directory to store our hello world project. Now we can setup a virtual environment. We will be suing pipenv to do this. ***NOTE*** If you don’t have pipenv installed you will need to install it by running

pip install pipenv

Now we will create our virtual environment

pipenv install django~=3.1.0
pipenv shell

These commands will install Django into our directory and then create a virtual environment. For those of you that are familiar with Node.js Pipenv will create file called Pipfile that stores all of the projects dependencies similar to the Package.json file that gets created when you run

npm init

If you are on a MacBook or Linux machine you will see (helloworld) in parenthesis indicating that you are in the virtual environment. You can also run the ```tree``` command to see the directory printed out in the console. Windows users you will unfortunately either need to be using WSL(Windows Subsystem for Linux) or use Cygwin. I haven’t tested the tree command on Cygwin but I can attest to the tree command working on WSL.

After you run the command you should see

Pipfile
Pipfile.lock
config
|_ __init__.py
|_ asgi.py
|_ settings.py
|_ urls.py
|_ wsgi.py
manage.py
  1. Config is a directory which stores five files inside. The one that is of most importance at this stage is the settings.py file. This file controls the projects settings.
  2. urls.py tells Django which pages to build in response to a browser or URL request.
  3. wsgi.py, which stands for Web Server Gateway Interface, helps Django server our eventual web page.
  4. manage.py is used to execute Django commands such as running the local web server or creating a new app.
  5. asgi.py file and it’s new to Django as of version 3.0+ which allows for an optional Asynchronous Server Gateway Interface to be run. We won’t be using the asgi.py file here. Don’t delete it though.

Django comes with a built-in web server for local development purposes. We can run ```python manage.py runserver``` which will create a web server for you to view the application on http://localhost:8080. You should see the Django welcome page:

If you have done some web programming in the past you will notice that there are a few warnings in your terminal currently but you can ignore them. Nothing is broken on the project. The errors are indicating that you have migrations for your models that need to be updated for the database. Since we aren’t using any models in this sample project and you don’t have a database either we can safely ignore these.

Let’s crate an app now for our Django project. Django uses projects and apps to keep code clean and readable. A single Django project contains one or more apps within it that all work together in tandem to create a web application.

Run ```python manage.py startapp hello```

Again using the tree command you should see the new directory that was created this time it will say

hello
|_ __init__.py
|_ admin.py
|_ apps.y
|_ migrations
|_ __init__.py
|_ models.py
|_ tests.py
|_ views.py

Let’s go over these real quick.

  1. admin.py — a configuration file for the built-in Django Admin app
  2. apps.py — a configuration file for the app itself
  3. migrations — directory to keep track of any changes to our models.py file
  4. models.py — we define our database models here which Django automatically translates into database tables
  5. tests.py — for any app specific tests
  6. views.py — where we handle the request/response logic for our web app

Open up the config/settings.py file and add our new hello project to the end of the INSTALLED_APPS section like this:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contnenttypes',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello',

Next we will open up the views.py file and input this code:

from django.http import HttpResponsedef homePageView(request):
return HttpResponse("Hello from Django!")

Create a new file in your hello directory called urls.py and inside of that add this code:

from django.urls import path
from .views import homePageView
urlpatterns = [
path("/", homePageView, name="home")
]

Next we will update the config/urls.py file with the code in bold

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('include('home.urls')),

After saving all the files we can run

python manage.py runserver

You should now see Hello, from Django! returned in the browser where you had the Django Home page before.

This is how easy web development is with Django. However it can be a bit tedious since you have to update the views.py file then the urls.py file then the settings.py file etc. It can be daunting. If you are looking for an alternative look no further than Flask.

Hello Flask

We are going to follow the same setup as before. We will use pipenv to install our dependencies as well as create our virtual enviornment. Let’s get started.

If you have the terminal up from before with our Django project you will need to change out of that directory. Here are the steps

exit
cd ..
mkdir hello_flask && cd hello_flask
  1. Exit is a new command this will deactivate the virtual environment that we created for Django.

Now we are in our newly created directory which we can install Flask into now and start our new virtual environment.

pipenv install Flask
pipenv install python-dotenv
pipenv shell

Our dependencies are installed and we are ready to rock and roll. Go ahead and run

tree
code .

You will notice that… the directory is empty. There aren’t any files or folders. Here is where using Flask can be a great thing. You don’t have additional files that you don’t know what they are for and you don’t have to plugin a bunch of settings before you can even start to write code. Now let’s start to write our app.

Create a two files one named app.py the other named .flaskenv either through VS Code or from the terminal using

touch app.py
touch .flaskenv

Inside of app.py write this code:

from flask import Flaskapp = Flask(__name__)@app.route("/")
def index():
return "Hello, from Flask!"

Inside of .flasenv write this code:

FLASK_APP=app.py
FLASK_ENV=development

After we import flask we need to create a Flask object. The typical way of doing that is assigning it to a variable named app or main. You can of course name it whatever you like. app or main are just the de facto.

Next we create the route. This is similar to the Django way of doing it where in views.py we created the function homePageView with a return statement to return Hello, from Django. From there we inside the Django app we needed to update the urls.py file so that we could specify the route at “/”.

Now save your files and in the terminal execute this command

flask run

You can now visit http://localhost:5000 and you will see our lovely message.

That’s it. Just two files and about 7 lines of code give or take. And technically you don’t have to use the .flaskenv file you could just run

export FLASK_APP=app.py - on linux and mac
set FLASK_APP=app.py - on Windows
# Then run
flask run

And you would have the same result. The only reason we opted for the .env file is because it makes it easier for you to not have to remember to set that every time you want to run your application. I also added the FLASK_ENV=development so that when you start your server it starts in debug mode so when you make changes it restarts your server when you save a file.

I hope that this article helped to showcase the differences between Flask and Django. As I mentioned at the beginning I use both frameworks. I wouldn’t say one is better than the other however I would say that in certain situations I do believe using one over the other has it’s advantages. I will leave that up to you dear reader on which one you would rather use for your projects.

--

--

Jonathan Reeves
Jonathan Reeves

Written by Jonathan Reeves

I am a software engineer that is currently trying to break into the DevOps world using Python. Professionally I use JavaScript with React to build websites.

No responses yet