Django
- Pronunciation
- JANG-goh
In short
Django is an open-source Python web framework that comes with an ORM, an admin panel, authentication and security protections, so teams can build quickly.
What is Django?
Django was created at a newspaper in Lawrence, Kansas, to build news sites on tight deadlines, and was released as open source in 2005. It follows a "batteries included" philosophy: instead of picking separate libraries for each job, you get a database layer, URL routing, templates, forms, authentication, an admin interface and caching in one coherent package.
A Django project is organized as models, views and templates. Models are Python classes that describe your data; Django's ORM turns them into database tables and lets you query them without writing SQL. Views are functions or classes that handle a request and return a response, and templates render HTML. Django calls this pattern MTV, its own version of MVC.
One of its most loved features is the automatic admin: from your models, Django generates a working back office where staff can add, edit and search data. It also protects against common attacks out of the box, including SQL injection, cross-site scripting, cross-site request forgery and clickjacking. For APIs, the Django REST Framework adds serializers and views for building REST endpoints.
A common misconception is that Django is only for small sites. It runs large services, including parts of Instagram, and scales with caching, read replicas and background workers. It is more opinionated than minimal frameworks such as Flask or FastAPI, which trade built-in features for flexibility.
Key takeaways
- Django is an open-source, "batteries included" Python web framework.
- Models, views and templates form its MTV structure.
- Its ORM maps Python classes to database tables.
- An automatic admin panel is generated from your models.
- Built-in protections cover SQL injection, XSS, CSRF and clickjacking.
Example
# models.py: Django creates the table from this class
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
published = models.DateTimeField(auto_now_add=True)
# views.py: query with the ORM, render a template
from django.shortcuts import render
def latest(request):
articles = Article.objects.order_by("-published")[:10]
return render(request, "latest.html", {"articles": articles})Readers ask
Is Django frontend or backend?
Backend. Django runs on the server, handles requests, talks to the database and returns HTML or JSON. It can render pages itself or serve an API for a separate frontend such as React.
What is the difference between Django and Flask?
Django includes an ORM, admin, authentication and more out of the box. Flask is a microframework that gives you routing and leaves the rest to extensions you choose.
What is Django REST Framework?
A popular library on top of Django for building web APIs. It adds serializers that convert models to JSON, views for REST endpoints, authentication and a browsable API.
Often compared
See also
- PythonProgramming Languages, p. 26Python is a general-purpose, dynamically typed programming language known for readable syntax and wide use in data science, automation, and web backends.
- FrameworkProgramming Fundamentals, p. 20A framework is a reusable foundation of code, tools, and conventions that provides the structure of an application, so developers only fill in their own logic.
- ORMDatabases, p. 33An ORM is a library that maps database tables to objects in your programming language, letting you read and write data with code instead of raw SQL.
- MVCSoftware Architecture, p. 29MVC is an architectural pattern that splits an application into a Model for data and logic, a View for display, and a Controller that handles user input.
- REST APIBackend & APIs, p. 38A REST API is a web API that exposes data as resources identified by URLs and lets clients read or change them using standard HTTP methods.
- CSRFSecurity, p. 8CSRF is an attack that tricks a logged-in user's browser into sending an unwanted request to a trusted site, which treats it as a genuine user action.
- FlaskBackend & APIs, p. 18Flask is a lightweight Python web framework offering routing, request handling and templates, leaving the database and project structure to the developer.
Spotted a mistake or something missing on this page?Suggest an edit