Django is a powerful Python web framework used to build modern, secure, and scalable web applications. It provides many built-in features that make web development faster and easier.
Django is a high-level Python web framework. It provides tools and features for creating web applications without having to build every part of the application from scratch.
Django can be used to create websites, dashboards, content management systems, educational platforms, e-commerce applications, APIs, and many other types of web applications.
Python + Django = Web Application
Django is built using Python. Therefore, basic knowledge of Python is very useful before learning Django.
For example, a simple Python function can be written as:
def welcome():
return "Welcome to Django"
Django uses Python code to handle application logic, database operations, URLs, forms, authentication, and other functionality.
Django provides many features required for web development. This allows developers to concentrate more on application functionality instead of repeatedly creating common features.
Django is open-source software. Its source code is publicly available and developers can use it according to its license.
An open-source framework also has a large developer community that creates documentation, packages, tutorials, and solutions.
Django can be used to develop many different types of web applications.
Django follows an architectural pattern commonly described as MTV.
These components separate database operations, presentation, and application logic.
A Django Model represents data and is normally mapped to a database table.
For example, a Student model could contain fields such as name, email, mobile number, and course.
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
course = models.CharField(max_length=100)
A Template is used to create the presentation layer of a Django application. Django templates commonly contain HTML along with Django Template Language syntax.
<h1>Welcome to Django</h1>
<p>
This is my first Django page.
</p>
Templates help separate the page design from application logic.
A View contains the logic that processes a request and returns a response.
from django.http import HttpResponse
def home(request):
return HttpResponse(
"Welcome to Django"
)
The view can receive information from the request and return HTML, JSON, redirects, or other responses.
Django uses URL patterns to connect web addresses with views.
from django.urls import path
from . import views
urlpatterns = [
path(
"",
views.home,
name="home"
),
]
When the requested URL matches a pattern, Django can call the associated view.
A web application generally receives a request from a client and returns a response.
Browser
↓
Django URL
↓
View
↓
Response
↓
Browser
The request object contains information about the incoming
HTTP request.
Django provides an Object-Relational Mapper, commonly called the ORM. It allows Python code to work with database records using Django models and QuerySets.
Django supports several database systems through its database backends, including SQLite, PostgreSQL, and MySQL.
Student.objects.all()
The above expression can be used to obtain Student records through the Django ORM.
ORM stands for Object-Relational Mapping. Django ORM allows developers to work with database data using Python objects and QuerySets.
students = Student.objects.all()
for student in students:
print(student.name)
This reduces the need to write SQL manually for many common database operations.
Django includes a built-in administration interface that can be used to manage application data.
After creating a model and registering it with the admin site, authorized administrators can manage records through the admin interface.
from django.contrib import admin
from .models import Student
admin.site.register(Student)
Django includes authentication functionality for managing users, passwords, login sessions, permissions, and related features.
Authentication can be used to create systems such as:
Django provides a form system that helps developers create and validate HTML forms.
from django import forms
class StudentForm(forms.Form):
name = forms.CharField(
max_length=100
)
email = forms.EmailField()
Django forms can help handle input validation and cleaned form data.
Django provides several security mechanisms and secure defaults to help developers build safer applications.
Important security areas include:
Middleware is a framework of hooks that can process requests and responses globally or perform other functions during request processing.
Examples include middleware related to:
Static files include CSS, JavaScript, and images used by a web application.
Django provides support for organizing and serving static files during development and collecting them for deployment.
STATIC_URL = "static/"
A template can load the static template tag library to reference static files.
{% load static %}
<link
rel="stylesheet"
href="{% static 'css/style.css' %}">
Django templates can display dynamic values supplied by a view.
For example, a view can send a name to a template:
def home(request):
context = {
"name": "Rahul"
}
return render(
request,
"home.html",
context
)
The template can display the value using:
<h1>
Hello {{ name }}
</h1>
Django Template Language provides template tags for performing template-level operations.
For example, an if tag can conditionally display content.
{% if user.is_authenticated %}
<p>
Welcome back!
</p>
{% endif %}
The for template tag can be used to display multiple
items.
{% for student in students %}
<p>
{{ student.name }}
</p>
{% endfor %}
This is useful for displaying database records in HTML pages.
Template filters modify or format values before displaying them.
{{ name|upper }}
The upper filter converts text to uppercase in the
template output.
Other filters can be used for formatting, default values, dates, lengths, and other operations.
A Django project is the overall configuration and collection of applications that make up a Django website.
A project can contain one or more Django applications.
myproject/
│
├── manage.py
│
└── myproject/
├── settings.py
├── urls.py
├── asgi.py
└── wsgi.py
A Django application is a Python package that provides a specific functionality within a project.
For example, a student management project could contain applications such as:
Applications help organize a larger Django project into manageable parts.
Django provides the manage.py command-line utility for
performing many development and administration tasks.
For example, a development server can be started with:
python manage.py runserver
Other commands are used for creating applications, making migrations, applying migrations, creating superusers, and more.
Migrations allow Django to record and apply changes to database schemas based on model changes.
Two commonly used commands are:
python manage.py makemigrations
python manage.py migrate
The first command creates migration files based on detected model changes, while the second applies migrations to the database.
Django includes a lightweight development server for testing an application during development.
python manage.py runserver
After starting the server, the development site is commonly available at:
http://127.0.0.1:8000/
A simplified Django request workflow can be understood as:
User
↓
URL
↓
URL Pattern
↓
View
↓
Model / Database
↓
Template
↓
HTTP Response
↓
User
Not every request follows exactly the same path, but this model is a useful starting point for understanding how common Django applications are structured.
Throughout this 60-lesson Django tutorial, you will progressively learn how to create complete Django applications.
manage.py is used for many Django management tasks.Question: Django is primarily a web framework for which programming language?