From Django to Flask by Abrar Wali

From Django to Flask

Abrar Wali
5 min readMar 30, 2022

--

Django and Flask are the most widely used Python web frameworks. If you are familiar with Django framework, and thinking of adding Flask to your skill set, then you’ve come to the right place as I am going to try to make this process a little less grinding for you. We’ll go through the basic differences between Flask and Django. Having some insight about what is different in Flask in comparison to what you already know (Django) should give you an idea what to expect in your journey to learn Flask.

With simple examples, we will attempt to understand the difference in how Django and Flask work and if there are any similarities in the working of both of these Frameworks. Please note that the objective of this article is not to teach Django or Flask and neither to compare the features of the two frameworks, but simply to understand how Flask works in comparison to Django.

Let’s Begin

Django’s project structure is most often described according to the Model-View-Controller (MVC) architecture because it makes the framework easier to learn.

  • The Model controls the Organization and Storage of data. The Models are a representation of your Database in Django (Models.py).
  • Views are how everything is represented to the user, In Django it means our templates (HTML templates).
  • Controller is the part where the developers define logic such as how to process requests, tie several parts together and so on (Views.py).

Let’s take an example.

The user enters any URL. Django processes this URL by looking up the patterns one by one in url_patterns defined in our urls.py file. The URL pattern that matches the entered URL invokes a function or a ClassBasedView (CBV) in Views.py file. The functions or Class Based Views is where the developer defines all the logic to handle requests and data. It will take a defined action on an incoming request. This action can be a query to the Database, data processing, responding with an HTML template, a combination of these or some other developer defined action.
Abstracting all the complexities away, this is a simple explanation of how Django Works.

Now let’s see how the same process is done in Flask when a URL is entered by the user, but before we continue, I want to talk about Flask App Structure. Why? Because how your flask app is structured will define the flow of the code you write.

Flask App Structure

By Default, Flask will create an app.py file where you are to write your code. But unlike in Django where you have separate files such as urls.py, views.py and models.py, the flask provides all these functionalities in a single file (app.py). You can choose how to structure your app based on your personal preferences. I prefer creating a py file separately for writing my models, configurations, my routes etc (basically changing the app structure to what we are familiar with in Django). It makes engaging with different parts of the project easy, and you are writing clean code. You can check the structure I implemented in a dummy project a while ago here.

If you like tutorials, Corey Schafer has a video on how to structure your Flask App. It’s a part of his Flask Series. You can check his YouTube channel here.

Let’s continue

Similar to Django, we can define models in Flask but unlike Django which has a built-in support for writing models out of the box (Django ORM), we have to rely on SQLAlchemy to define our models. For Flask, we use the package flask_sqlalchemy. We can also choose to write plain SQL for defining our models, but using flask_sqlalchemy is easier.

In Django, as we are already aware, url_patterns are looked up and the matched url_pattern calls a function or a CBV in views.py. In Flask, we achieve the same result with app routing. It is used to map the specific URL with the associated function that is intended to perform some task.

@app.route(“/home”)
def home():
print(“Welcome to Flask”)

@app.route() is a Python decorator that Flask provides to assign URLs in our app to functions easily. In the above example, when the home URL is entered by the user, home function will be called. Similarly, every function has a URL associated with it in the form of decorators. As in Django, there are usually multiple functions with decorators in a Flask project for different routes. A route is what you would call a URL pattern in Django.

Templates in Flask are used same as in Django with Jinja Templates. Jinja templates make use of special placeholders. These placeholders can be replaced with auto generated HTML content. The placeholders read data served by the Flask App.

A bit of Django Vs Flask to conclude this topic

Django advertises itself as a Web Framework for perfectionists with deadlines, while the Flask advertises itself as a minimalistic Web Framework. To make it more clear, Django as you know allows you to write less code and build apps quickly without having to worry about the technical jargon that comes with web development. As mentioned on their website, it’s ridiculously fast, exceedingly scalable and reassuringly secure so the developers can quickly finish writing the app without having to worry about security and scalability (perfectionists with deadlines). Now, Flask likes to keep things simple and let the developer make decisions themselves. Flask has extension support, which allows developers to add only those tools that are needed in a project. For example, by default Flask does not include a database abstraction layer, form validation or anything else i.e. the tools that are needed by the developers should be integrated to the flask by the developers. This can be done by using pip package manager. There are a lot of articles available on the distinctions between Django and Flask on the internet, but for someone who is coming from Django, I wanted to shed some more light on the topic and not write yet another distinction article.

I hope this article proves to be helpful to you. All the best for your Flask journey ;)

--

--

Abrar Wali

Computer Science Engineer | Linux & Open Source Enthusiast