Lesson 1 of 60 – Introduction to Django
2%

Introduction to Django

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.

Note: Django is written in Python and follows the Model-Template-View (MTV) architectural pattern.

1. What is Django?

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

2. Django is a Python Framework

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.

3. Why Learn Django?

Django provides many features required for web development. This allows developers to concentrate more on application functionality instead of repeatedly creating common features.

  • Fast development.
  • Built-in database support.
  • URL routing.
  • Template system.
  • Authentication.
  • Forms and validation.
  • Security features.
  • Administration interface.

4. Django is Open Source

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.

Important: Open source does not mean that every third-party package is licensed in exactly the same way. Always check the license of packages used in a project.

5. Django Web Applications

Django can be used to develop many different types of web applications.

  • Business websites.
  • Educational websites.
  • Blogging platforms.
  • Student management systems.
  • Library management systems.
  • E-commerce applications.
  • Booking systems.
  • REST APIs.
  • Admin dashboards.

6. Django Follows MTV Architecture

Django follows an architectural pattern commonly described as MTV.

  • M = Model
  • T = Template
  • V = View

These components separate database operations, presentation, and application logic.

7. Model in Django

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)

8. Template in Django

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.

9. View in Django

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.

10. URL Routing

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.

11. Django Request and Response

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.

12. Django and Databases

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.

13. 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.

14. Django Admin Panel

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)

15. Django Authentication

Django includes authentication functionality for managing users, passwords, login sessions, permissions, and related features.

Authentication can be used to create systems such as:

  • Student login.
  • Teacher login.
  • Admin login.
  • User registration.
  • Protected pages.

16. Django Forms

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.

17. Django Security

Django provides several security mechanisms and secure defaults to help developers build safer applications.

Important security areas include:

  • CSRF protection.
  • Clickjacking protection.
  • Host validation.
  • Password hashing.
  • SQL injection protection through the ORM.
  • Security-related middleware.
Important: Security also depends on correct application configuration and secure development practices.

18. Django Middleware

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:

  • Security.
  • Sessions.
  • Authentication.
  • Messages.
  • CSRF protection.

19. Django Static Files

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' %}">

20. Django Templates and Dynamic Data

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>

21. Django Template Tags

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 %}

22. Django Template Loops

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.

23. Django Template Filters

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.

24. Django Project

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

25. Django Application

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:

  • students
  • courses
  • payments
  • attendance

Applications help organize a larger Django project into manageable parts.

26. Django Management Command

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.

27. Django Migrations

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.

28. Django Development Server

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/
Important: The Django development server is intended for development and testing, not as a production web server.

29. Simple Django Workflow

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.

30. What You Will Learn in This Course

Throughout this 60-lesson Django tutorial, you will progressively learn how to create complete Django applications.

  • Django fundamentals.
  • Project and application structure.
  • URLs and views.
  • Templates.
  • Static files.
  • Models and databases.
  • Django ORM and QuerySets.
  • CRUD operations.
  • Relationships.
  • Forms and validation.
  • User authentication.
  • Sessions and messages.
  • File and image uploads.
  • Pagination and search.
  • REST API fundamentals.
  • Practical Django projects.
  • Complete website development.
Great Start! You now understand the basic idea of Django, its MTV architecture, projects, applications, URLs, views, templates, models, and major built-in features.

📌 Key Points

  • Django is a high-level Python web framework.
  • Django is used to build dynamic web applications.
  • Django follows the Model-Template-View architecture.
  • Models generally represent database data.
  • Templates are used for presentation.
  • Views handle request processing and return responses.
  • URL patterns connect URLs with views.
  • Django provides an ORM for database operations.
  • Django includes a built-in administration interface.
  • Django provides authentication and form functionality.
  • Django includes several security mechanisms.
  • manage.py is used for many Django management tasks.
  • Migrations help manage database schema changes.
  • Django supports reusable applications within a project.

🧠 Quick Quiz

Question: Django is primarily a web framework for which programming language?