In this guide, you will learn how to create a new Django project, understand its project structure, and launch a web application using Django's built-in development server.

Overview of Django

Django is a high-level Python web framework designed to help developers build secure and maintainable websites quickly. It promotes rapid development, clean design, and adheres to the DRY (Don't Repeat Yourself) principle, which encourages reusability and minimal redundancy.

Django follows the Model-View-Template (MVT) architectural pattern, a variant of the traditional Model-View-Controller (MVC) pattern. Here is how its core components map:

  • Model: Defines the data structure and handles interactions with the database.

  • View: Manages the logic and retrieves data through the model.

  • Template: Defines how data is presented in the web browser.

The Django framework acts as the controller, routing requests using URL patterns.

MVC MVT
Model Model
Controller View
View Template

Django Architecture: HTTP Request/Response Cycle

  1. The browser sends an HTTP request to the web server.

  2. The web server forwards the request to Django.

  3. Django matches the request against URL patterns.

  4. The corresponding view is called.

  5. The view uses a model to retrieve data.

  6. The data is passed to a template.

  7. The template is rendered and sent back as an HTTP response.

Creating a Virtual Environment

Using a virtual environment allows you to isolate project dependencies. This is especially useful when working on multiple projects or deploying to different environments.

# Create a project directory
mkdir django-playground
cd django-playground

# Create a virtual environment
python -m venv venv

# Activate the virtual environment (Windows)
venv\Scripts\activate

# Deactivate it later with
deactivate

Installing Django

After activating the virtual environment, install Django using pip:

pip install django

# Verify installation
python -m django --version

Using Django Commands

Django includes a command-line utility called django-admin for administrative tasks. To view available commands:

django-admin

Creating a Django Project

To create a new Django project, use the startproject command:

django-admin startproject django_project
cd django_project

Project Structure

django_project/
├── manage.py
└── django_project/
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    ├── wsgi.py
    └── asgi.py
  • manage.py: Tool for managing the project.

  • __init__.py: Declares the directory as a Python package.

  • settings.py: Configuration file for the project.

  • urls.py: URL declarations.

  • wsgi.py: Entry point for WSGI-compatible web servers.

  • asgi.py: Entry point for ASGI-compatible web servers.

Running the Development Server

To launch the development server:

python manage.py runserver

Output:

Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Open the URL in your browser to view the default Django welcome page.

Admin Panel

To access the admin interface:

# urls.py
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

Visit: http://127.0.0.1:8000/admin

Stopping the Server

To stop the development server, press Ctrl-C in the terminal.

Creating a requirements.txt File

To list all installed packages and their versions:

pip freeze > requirements.txt

To install dependencies from this file on another system:

pip install -r requirements.txt

Creating a Django Application

A Django project can contain multiple applications. Applications are modular components containing models, views, templates, and URL configurations.

Creating the Blog App

Run the following command to create a new application:

python manage.py startapp blog

This creates a blog/ directory with the following files:

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

Registering the Application

Open settings.py and add 'blog.apps.BlogConfig' or 'blog' to the INSTALLED_APPS list:

INSTALLED_APPS = [
    # ...
    'blog.apps.BlogConfig',
]

Creating Views

Open blog/views.py and define a simple view:

from django.http import HttpResponse

def home(request):
    return HttpResponse('<h1>Blog Home</h1>')

To add more views:

def about(request):
    return HttpResponse('<h1>About</h1>')

Configuring URLs

Create a urls.py file inside the blog directory:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='posts'),
    path('about/', views.about, name='about'),
]

Now, include the blog's URLs in the main urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
]

To make the blog app the homepage:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]

Start the server:

python manage.py runserver

Visit:

Summary

  • Django is a robust Python framework that simplifies web application development.

  • It follows the MVT pattern, similar to MVC.

  • Use django-admin startproject new_project to create a project.

  • Use python manage.py startapp app_name to create applications.

  • Define views in views.py and routes in urls.py.

  • Include application URLs using include() in the main project routing.

  • Run the server with python manage.py runserver and stop with Ctrl-C.

  • Use pip freeze > requirements.txt to manage dependencies.