Django Introduction – Learn Python Web Framework

Introduction

Django is one of the most popular and powerful Python web frameworks used for developing secure, scalable, and high-performance web applications. It helps you build modern websites, web applications, and backend systems quickly.

Django follows a “batteries-included” approach, meaning it comes with many built-in features such as authentication, database management, URL routing, an admin panel, security features, and form handling.

What is Django?

Django is an open-source, high-level Python web framework used for rapid development of secure and maintainable web applications.

Django follows the MVT (Model-View-Template) architectural pattern, which separates application logic, database operations, and user interface.

Django was created in 2003 by developers at the Lawrence Journal-World and was publicly released in 2005. It is maintained by the Django Software Foundation.

Features of Django

Django provides many powerful features.

1. MVT Architecture

Django follows the Model-View-Template pattern.

2. Object-Relational Mapping (ORM)

Django ORM allows developers to interact with databases using Python code instead of SQL queries.

Example:


User.objects.all()

3. URL Routing

Django provides a simple URL mapping system.

Example:


path('about/', views.about)

4. Authentication System

Django includes built-in features for:

  • User registration
  • Login
  • Logout
  • Password management

5. Admin Interface

Django automatically creates an admin dashboard.

Developers can manage:

  • Users
  • Database records
  • Application data

6. Template System

Django templates allow developers to create dynamic HTML pages.

Example:


<h1>
Welcome {{ username }}
</h1>

7. Security Features

Django provides built-in security mechanisms.

Django Architecture (MVT)

Django follows the Model-View-Template architecture.


         User
           |
           ↓
        URL Request
           |
           ↓
          View
        /       \
       ↓         ↓
    Model     Template
       |
       ↓
   Database

Django MVT Components

1. Model

The Model handles database-related operations.

It defines:

  • Database tables
  • Fields
  • Relationships

Example:


class Student(models.Model):
    name = models.CharField(
        max_length=100
    )

2. View

The View contains application logic. It receives requests and returns responses.

Example:


def home(request):
    return HttpResponse(
        "Hello Django"
    )

3. Template

Templates handle the presentation layer.

Example:


<h1>
Hello Django
</h1>

How to Install Django?

Before creating Django applications, install Django using pip.

1. Installation Command


pip install django

2. Verify Django Installation


django-admin --version

Output:

5.x.x

Creating Your First Django Project

Use the Django command:


django-admin startproject myproject

This creates a new Django project.

Django Project Structure

After creating a project:


myproject/
│
├── manage.py
│
└── myproject/
    │
    ├── settings.py
    ├── urls.py
    ├── asgi.py
    └── wsgi.py

Understanding Django Files

1. manage.py

Used to manage Django commands.

Examples:


python manage.py runserver

2. settings.py

Contains project configuration:

  • Database settings
  • Installed apps
  • Middleware
  • Security settings

3. urls.py

Handles URL routing.

Example:


urlpatterns = [
]

4. wsgi.py

Used for deploying Django applications.

5. asgi.py

Used for asynchronous Django applications.

Running Django Development Server

Navigate to the project folder:


cd myproject

Run:


python manage.py runserver

Output:

Starting development server at

http://127.0.0.1:8000/

Open the URL in your browser, You will see The install worked successfully!

Creating a Django Application

A Django project can contain multiple applications.

Create an app:


python manage.py startapp blog

Django App Structure


blog/
├── admin.py
├── apps.py
├── models.py
├── views.py
├── tests.py
└── migrations/

Creating First Django View

Open:


views.py

Add:


from django.http import HttpResponse
def home(request):
    return HttpResponse(
        "Welcome to Django"
    )

Adding URL Configuration

In:


urls.py

Add:


from django.urls import path
from . import views
urlpatterns = [
    path(
        '',
        views.home
    )
]

Django Database Migration

Django uses migrations to manage database changes.

Create migrations:


python manage.py makemigrations

Apply migrations:


python manage.py migrate

Django Admin Panel

Create admin user:


python manage.py createsuperuser

Enter:


Username
Email
Password

Run server:


python manage.py runserver

Visit:


http://127.0.0.1:8000/admin/

Django ORM Example

Instead of SQL:


SELECT * FROM students;

Django uses:


Student.objects.all()

Advantages:

  • Less code
  • Database independent
  • Easy maintenance

Django vs Flask

Feature Django Flask
Type Full Framework Micro Framework
Architecture MVT Flexible
Built-in Features Many Limited
Learning Curve Moderate Easy
Best For Large Applications Small Projects/APIs

Common Django Mistakes

1. Forgetting to Add App in settings.py

Add:


INSTALLED_APPS = [
    'blog',
]

2. Not Running Migrations

Solution:


python manage.py migrate

3. Incorrect URL Configuration

Check:


urls.py

4. Database Errors

Verify:

  • Database configuration
  • Migration status

5. Poor Project Structure

Separate:

  • Apps
  • Templates
  • Static files

Best Practices

1. Use a Virtual Environment

Create:


python -m venv venv

2. Follow Django Naming Conventions

Use meaningful names.

3. Keep Applications Separate

Create different apps for different features.

4. Secure Settings

Never expose:

  • Secret keys
  • Database passwords

5. Use Django Built-in Features

Avoid rewriting existing functionality.

Conclusion

Django is one of the most powerful Python frameworks for developing modern web applications. Its built-in features, security mechanisms, ORM system, and MVT architecture make web development faster and more efficient.

Whether you are building a small website or a large-scale application, Django provides the tools needed for professional web development.

Related Python Tutorials