Django vs Flask in 2017 -- Words (952)

Django Flask

The Python language has many great frameworks for building web applications among them Django and Flask .

Both frameworks are gaining more popularity steadily in time and there are no signs they are becoming less popular or obsolete even if the web has known many important changes since the data they have been created .

Django was created in 2005 while Flask was created in 2010 .Today we are in 2017 which means Django is 12 years old and Flask is 7 years old .They are both extremely popular frameworks to build web applications with Python but they are so different in principle from each other .While Django follows a battery included approach making it a full packed ,complete and an opinionated framework ,on the other hand Flask is a micro framework ,an un-opinionated framework that let you choose what tools you can use for building a web app from the ORM to the templating engine .

In this post we'll discuss some points you might want to consider if you need to learn Django or Flask or maybe both ? and which framework you should consider using to build your next project ? and in which situations ?

They are both so popular so there are more and more websites built using both of them and more job demands for both frameworks .

Flask is a simple and flexible and lets you decide how to implement each major component such as the ORM to interact with databases ,the template engine ,forms etc .

Django is a little bit heavy when compared to Django .It has its own ORM , a template engine and even an admin back office out of the box .

Django has a predefined project directory structure while when you can structure a Flask project as you want .

When ypu should choose Flask ?

If you are just beginning web development with Python then Flask is a good option for learning purposes since you can have more control of all components of the framework .

Also if you are building small web applications then Flask can be also your to go choice .

How about Django ?

If you are a professional developer who needs to build quick prototypes or final products Django might be a good choice since it's a batteries included framework .There is a package for common web development tasks so you don't have to reinvent the wheen or waste your time building what other developers have already created .Just include the batteries and build your specefic requirements above them .

The Django ORM is easy to grasp and straightforward and lets you express your businness domain requirements clearly then you have the Django admin interface ,a complete web application that lets you do crud operations on your models such as creating ,updating ,deleting and views database records from an intuitive user interface generated for you on the fly without writing more code .

So thanks to batteries Django is your choice for either quick prototypes and final products .

Now lets see how we can create a simple Hello World web application in both frameworks :

Starting with Flask .Create a Python file

    touch webapp.py

Import Flask from flask

from flask import Flask         

Create an instance

app = Flask(__name__)

Create a view function which responds to HTTP requests and a route /

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

This function responds with response "Hello, World!" when thr user visit / route .

Next

if __name__ == "__main__":
app.run()  

We call the app run() method to start the web app .

Now you can run this app from terminal :

python webapp.py 

You should get

Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

If you visit this address with a web browser you should get a web page with Hello, World! response

For Django ,you should first generate a project with

django-admin startproject djwebapp 
cd djwebapp 

Next you should create an app

python manage.py startapp myapp

Then change your settings.py to include this app in Installed Apps array .

Next open myapp/views.py and create a view function

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, World!")

After that you should open your project urls.py file and add an url mapping for this view function

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

You should now be able to run your web app from the terminal

python manage.py runserver

As you can see you can use Flask to rapidly create an example web app that receives http requests and responds with http responses but you need to do more work if you just to make the same example with Django .

Choosing the right framework depends on many criteria ,you should take into consideration your goals , ara you just learning server side web development or you are building a project for a client ? you should also consider your project requirements ,some projects may be better developed in Django ,some other projects can be better created in Flask .Also remember if you need grained control over each part of your framework or you need to swap and makes use of different existing tools you should use Flask where you only have the bar minimum functionality and you have to choose other components by yourself .



mrnerd is a Web Entrepreneur & Developer, Digital Marketer and a Passionate Blogger. He writes about software and website development,money making tips and SEO etc.. He is a tech addict and a computer geek with a lot of hobbies.