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.
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?
- 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.
- 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.
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.
- Non-full-stack - Flask, Bottle, CherryPy, Pyramid
- Full-stack - Django, Tornado, TurboGears, web2py
It'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.
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 allows for building RESTful APIs for both web and mobile apps.
Is Django suited for real-time services and asynchronous 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
unittestmodule 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.
- Avram, Abel. 2014. "Comparing the Performance of Various Web Frameworks." InfoQ. May 2. Accessed 2017-11-01.
- Big-nige. 2017. "Why Django?" The Django Book Tutorials. May 9. Accessed 2017-10-31.
- Bogdanov, Vladimir. 2015. "Top 10 sites built with Django Framework." September 14. Accessed 2017-10-31.
- Brown, Ryan. 2015. "Django vs Flask vs Pyramid: Choosing a Python Web Framework". AirPair. Accessed 2017-11-01.
- Chand, Sai Deep. 2015. "What is the technology stack behind Pinterest?" Quora. July 31. Accessed 2017-10-31.
- DigitalOcean. 2013a. "Django Server Comparison: The Development Server, Mod_WSGI, uWSGI, and Gunicorn." DigitalOcean. August 19. Accessed 2017-11-01.
- DigitalOcean. 2013b. "How to Deploy Python WSGI Applications Using uWSGI Web Server with Nginx." DigitalOcean. December 11. Accessed 2017-11-01.
- Django Project. 2011. "Django 1.3 release notes." Django Project. March 23. Accessed 2017-10-31.
- Django Project. 2013. "Django 1.5 release notes." Django Project. February 26. Accessed 2017-10-31.
- Django Project. 2017a. "Django 1.11 release notes." Django Project. April 4. Accessed 2017-10-31.
- Django Project. 2017b. Django Project. Accessed 2017-10-31.
- Django Project. 2017c. "Why Django?" Django Project. Accessed 2017-10-31.
- Django Project. 2017d. "FAQ: General." Django Project. Accessed 2017-10-31.
- Django Project. 2017e. "Databases." Django Project. Accessed 2017-10-31.
- Django Project. 2017f. "Multiple Databases." Django Project. Accessed 2017-10-31.
- Django Project. 2017g. "Performance and optimization." Django Project. Accessed 2017-10-31.
- Django Project. 2017h. "Testing in Django." Django Project. Accessed 2017-10-31.
- Django Project. 2017i. "Testing tools." Django Project. Accessed 2017-10-31.
- Django Project. 2017j. "How to deploy with WSGI." Django Project. Accessed 2017-10-31.
- Django Project. 2017k. "Django 2.0 release notes". Django Project, December 02. Accessed 2019-08-30.
- Django Project Wiki. 2017. "NoSqlSupport." Django Project Wiki. April. Accessed 2017-10-31.
- Holovaty, Adrian. 2005a. "Source code to djangoproject.com now available." Django Project. July 19. Accessed 2017-10-31.
- Holovaty, Adrian. 2005b. "Django in use at washingtonpost.com." Django Project. December 8. Accessed 2017-10-31.
- HotFrameworks. 2017. "Top Frameworks." Accessed 2017-07-09.
- Knupp, Jeff. 2012. "Learning Python via Django Considered Harmful." December 11. Accessed 2017-11-01.
- Kornatskyy, Andriy. 2012. "Python Fastest Web Framework." Mind Reference Blog. September 18. Accessed 2017-01-11.
- Lima, Leandro. 2015. "WSGI: The Server-Application Interface for Python." Toptal. Accessed 2017-11-01.
- Makai, Matt. 2017a. "Django." Full Stack Python. Accessed 2017-10-31.
- Mugayi, Timothy. 2017. "Which is the best Python web framework for beginners?" Quora. February 12. Accessed 2017-11-01.
- Primakovski, Bogdan. 2017. "17 Best Python Web Frameworks to Learn in 2017". SteelKiwi Blog. May 8. Accessed 2017-11-01.
- Python Wiki. 2017. "Web Frameworks for Python."
- Quora. 2011. "For a beginner in Python, which web framework would be the best to start learning first (Django, Flask, Pylons, etc.)?" Quora. Accessed 2017-11-01.
- Ravindran, Arun. 2013. "Real-time Applications and will Django adapt to it?" ArunRocks. November 1. Accessed 2017-07-09.
- Robenolt, Matt. 2013. "Scaling Django to 8 Billion Page Views." Discus Blog. September 24. Accessed 2017-11-01.
- Sidorenko, Vladimir. 2017. "Django vs Rails Performance." GearHeart. Janurary 22. Accessed 2017-11-01.
- The Django Book. 2017a. "Introducing Django." The Django Book. Accessed 2017-10-31.
- The Django Book. 2017b. "The Model-View-Controller Design Pattern." The Django Book. Accessed 2017-10-31.
- Yegulalp, Serdar. 2016. "5 wicked-fast Python frameworks you have to try." InfoWorld. October 24. Accessed 2017-11-01.
- Python web frameworks
- Python template engines
- Django ORM
- Django Project. 2017. "Django Documentation." Accessed 2017-10-31.
- DjangoGirls. 2017. "Django Girls Tutorial." Accessed 2017-10-31.
- Makai, Matt. 2017. "Full Stack Python." Accessed 2017-10-31.