# Django

## Summary


Django is a free open source web framework written in Python. Like any web framework, it simplifies the development of web applications. It's tagline refers to Django as "the web framework for perfectionists with deadlines". Django Software Foundation, which is a non-profit organization, maintains Django.

The following claims are made about Django: ridiculously fast, fully loaded, reassuringly secure, exceedingly scalable and incredibly versatile. Django's default installation can be extended with third-party packages, such as from Django Packages.

While meant primarily for fullstack web apps (often database driven), Django can be used to provide backend services for mobile apps via REST API calls, to build real-time apps or even a content management system.

## Discussion

### What does it mean to say that Django is "fully loaded"?

Django's default installation comes with capabilities common for typical web applications. This includes user authentication, content administration, database ORM, database schema migrations, URL routing, RSS feeds, a templating engine, forms, a caching framework, CSRF protection, and many more. Variously called as "fully loaded", "full stack", "out of the box" or "batteries included", they all mean the same thing: default installation contains lots of features that we need not download and install individually. 

This does not imply that developers are restricted to using only what Django ships. Django is flexible enough to allow developers to use their preferred alternatives. For example, developers can use their own templating engine or ORM.


### What are the advantages of Django?

Advantages include the following: 

    + Django is based on Python, which is a popular language that's easy to learn and powerful. Python works on any platform and is also open source.
    + Django's official documentation is comprehensive and includes easy to follow tutorials.
    + Django has an active community that contribute via open packages, tutorials, books and code snippets.
    + Because Django takes the "batteries included" approach, downloading it and getting started is quick and easy.
    + With Django's built-in admin interface, it's easy to organize model fields or process data.
    + Django adapts the well-known MVC architecture and therefore code can be written with clean abstractions.
    + Django community makes regular stable releases, some of which are Long Term Support (LTS) releases that get three years of support.

### What's the architecture used by Django?

Django uses what is called MTV Architecture: Model-Template-View. This is equivalent to the more well-known MVC architecture: Model-View-Controller. The traditional controller's job is done by the framework itself and hence the MTV nomenclature does not emphasize it. 

The components of MTV architecture as are follows: 

    + **Model** - Data access layer. This deals with access, validation and relationships among data. Models are Python classes that mediate between Django ORM and the database tables.
    + **Template** - Presentation layer. This deals with how data is displayed to the client.
    + **View** - Business logic layer. This is a bridge between models and templates. A view accesses model data and redirects it to a template for presentation. Unlike the definition of a view in traditional MVC architecture, a Django view describes which data you see, not how you see it. It's about processing, not presentation.

### What are the different backend databases supported?

Django officially supports PostgreSQL, MySQL, SQLite and Oracle Database Server. In addition, third-party vendors provide support to interface their databases to Django. These include SAP SQL Anywhere, IBM DB2, Microsoft SQL Server, Firebird and ODBC. A branch of Django development called Django-nonrel is already supports a couple of non-relational databases, MongoDB and Google App Engine. However, support for many more NoSQL are listed on the NoSqlSupport Wiki page. 

All these databases need to provide a driver in Python. Psycopg is a popular one for PostgreSQL. MySQLdb is a driver for MySQL but it does not support Python 3. Its equivalent driver for Python 3 is mysqlclient. All these drivers conform to Python DB API.

PostgreSQL is preferred by the Django community. A Django app can connect to multiple databases. 


### What are some popular large-scale websites using Django?

Django has been used in the following sites: Disqus, Bitbucket, Instagram, Mozilla Firefox, Pinterest, NASA, The Washington Post and Eventbrite. Pinterest later migrated to Flask due to their need for database sharding. 

 DjangoSites is a site that tracks websites that are built using Django. As of October 2017, more than 5000 sites are listed.


### How does Django compare against other web frameworks?

As of July 2017, Django was the sixth most popular web framework. When considering only Python-based frameworks, it was in the top position. When looking at stars and forks on GitHub, Django, Flask and Tornado come up on top. 

Among the Python-based frameworks are the following: 

    + **Non-full-stack** - Flask, Bottle, CherryPy, Pyramid
    + **Full-stack** - Django, Tornado, TurboGears, web2pyIt's been said that Flask is a microframework suited for small apps with simple requirements. For larger apps, Pyramid or Django may be more suitable. Pyramid allows developers to put together the right tools for their app and hence offers more flexibility than the "batteries included" approach of Django. For example, Django ORM comes out of the box but Flask and Pyramid let developers choose any ORM of their choice. With Tornado, developers can do asynchronous programming.


### Is Django recommended for a Python beginner?

Flask is probably easiest to learn for beginners. Django has some learning curve but this effort will be worthwhile in the long run when building complex apps. Developers have commented that Web.py and Bottle are suitable for beginners. 

It's important to know Python well before starting on any web framework. For example, it's been mentioned that trying to learn Python via Django is not the right approach. 


### Can you share some performance results of Django?

Performance of Django in terms of throughput is seen to be above average, while Bottle and Pyramid appear to lead the pack. We should keep in mind that performance metrics are many (speed, memory consumption, etc.) and may involve tradeoffs. 

A direct comparison between Django and Ruby on Rails done in January 2017 shows that on average Python is slower than Ruby by 0.7%. Benchmark tests on three different hardware show that CPPSP and Go are highly performant. Django does poorly in these tests. 

It's been claimed that Django sites have handled peak traffic of 50K hits per second. Discus has used an HTTP caching layer called Varnish to scale to 45K hits per second. They make the point that the bottleneck in their app is due to slow database queries or network latency rather than the web framework. 


### Can Django be used for mobile apps, REST APIs or microservices?

Django cannot be used for developing native mobile apps. However, backend services for a mobile app can be developed with Django. Django REST framework can be used to build RESTful APIs for both web and mobile (native or hybrid) apps.


### Is Django suited for real-time services and asynchronous apps?

There are frameworks such as Meteor (Node.js) and Derby (JavaScript) that have been designed for real-time services and event-driven model. Within the world of Python, there are frameworks that support real-time needs of modern apps. 

Django was not designed for real time. However, with suitable add-on packages such as Django Channels, it can be used for real-time apps. A curated list of Python asynchronous frameworks and libraries is also being maintained on GitHub. This includes Tornado, gevent, asyncio and Twisted. Celery addresses real-time requirements and has provision to integrate with Django.


### What is the best way to test a Django-based web app?

Django relies on `unittest` module of Python for testing but developers can choose to use other testing frameworks of their choice. Django offers a test client that allows us to programmatically sends requests and validate responses. It's recommended that testing should involve a combination of Django test clients and "in-browser" testing frameworks such as Selenium. 


### What is WSGI in the context of deploying a Django web app?

WSGI stands for Web Server Gateway Interface and is standardized as PEP 3333. It sits as a high-level interface between Python-based web applications and web servers. This improves portability of apps across different web servers that support WSGI. Because of WSGI, a particular choice of a Python web framework need not limit the deployment to only servers that support that framework. 

Django development environment comes with a lightweight server but for production such a server is not suitable. Django is a web framework that needs a separate web server. This is unlike some frameworks such as CherryPy that has a built-in WSGI server. 

Django documentation describes the steps to deploy the same app across different web servers: Apache with mod\_wsgi, Gunicorn or uWSGI. When deploying to the cloud, cloud providers usually share the deployment steps. For example, Nginx could be the frontend web server that interfaces with uWSGI application server, which in turn invokes the Django application. 

## Milestones

2003

Web programmers Adrian Holovaty and Simon Willison begin using Python to build web apps for Lawrence Journal-World newspaper. Django has its roots here. This also implies that Django was from the outset focused on solving real-world problems. 

Jul  
2005

Django is released to the public. The framework is named Django after jazz guitarist Django Reinhardt. 

Dec  
2005

First Django app launched at The Washington Post. 

Mar  
2011

Django 1.3 starts support for class-based views. 

Feb  
2013

Support for Python 3 is added. 

Apr  
2017

Django 1.11 is released. It's the final major release to have support for Python 2. This will be supported till April 2020. 

Dec  
2017

Django 2.0 is released with only Python 3.4+ support.