Web Frameworks for Python

1 minute read

There are a lot of web frameworks available to choose from. Yet there are only two web frameworks which are very popular among Python developers. There are Django and Flask. More details on other Python web frameworks can be check here.

Flask VS Django

Thus in the post, will present the comparison between the most popular web frameworks which are Flask and Django.

The biggest difference between Flask and Django is:

  • Flask implements a bare-minimum and leaves the bells and whistles to add-ons or to the developer
  • Django follows a “batteries included” philosophy and gives you a lot more out of the box.

In the nutshell the comparison can be presented in analog like Flask is more like a pirates and Django is more like a Navy.

  • Django released in 2005 and Flask released in 2010

Flask

How to install Flask

pip install flask

Create a Python file called flaskhelloworld.py and insert the following code

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
  return "Hello, World!"
  
if __name__ == "__main__":
  app.run()

Run the command

python3 flaskhelloworld.py

Application is running on and ‘127.0.0.1’ means that the application is running on local host — it’s only accessible on our development machine. If you open a web browser and visit http://127.0.0.1:5000/, you’ll see a web page that returns the “Hello, World!” greeting.

Django

pip install django

Set up the django-admin command. Run the following:

django-admin startproject hellodjango
python3 manage.py startapp helloworld

Open helloworld/views.py and the following code:

from django.http import HttpResponse

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

From the hellodjango directory, run the following command:

python3 manage.py startapp helloworld

In the hellodjango directory too, open the automatically created helloworld/views.py file and add the following code:

from django.http import HttpResponse

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

Also need to create a urls.py file for the application. Create helloworld/urls.py and add the following code:

from django.conf.urls import url

from . import views

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

hellodjango directory (the one which contains the manage.py file) and run the following command:

python3 manage.py runserver