← Back to Chapters

Django Tutorial

? Django Tutorial

⚡ Quick Overview

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It helps you build robust, scalable, and secure web applications quickly by handling common tasks like URL routing, database interaction, templates, and authentication for you.

In this tutorial, you will see how to install Django, create a project, create an app, and display a simple “Hello, Django!” page in the browser.

? Key Concepts

  • Project – The overall Django configuration for your site (settings, URLs, etc.).
  • App – A module that does one specific job inside the project (blog, shop, accounts, etc.).
  • View – Python function or class that receives a request and returns a response.
  • URL Routing – Maps a URL (like / or /about/) to a specific view.
  • Development Server – Local web server used for testing during development.

? Syntax and Flow

A basic Django flow looks like this:

  1. The browser sends a request to a URL.
  2. Django checks the URL against patterns defined in urls.py.
  3. The matched URL calls a view function from views.py.
  4. The view returns an HttpResponse object (often with HTML content).
  5. Django sends this response back to the browser.

You organize your code into apps, and each app can have its own views.py, urls.py, models, templates, and more.

? Basic Setup and Commands

? Install Django

First, install Django (preferably inside a virtual environment):

? View Code Example
# Install Django in your environment
pip install django

? Create a Django Project

Use django-admin to create a new project, move into the project folder, and run the development server:

? View Code Example
# Create a project, enter it, and start the dev server
django-admin startproject myproject
cd myproject
python manage.py runserver

After running the server, open http://127.0.0.1:8000/ in your browser. You should see the default Django welcome page.

? Create a Django App

Inside your project, create an app where you will write your views and other logic:

? View Code Example
# Create a new Django app called "myapp"
python manage.py startapp myapp

Remember to add 'myapp' to INSTALLED_APPS inside settings.py so Django knows about it.

? Code Example: Simple Home Page

Now let’s define a simple view and connect it to a URL. This will display "Hello, Django!" in the browser.

? View Code Example
# myapp/views.py
from django.http import HttpResponse

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

# myproject/urls.py
from django.urls import path
from myapp import views

urlpatterns = [
    path('', views.home),
]

? Live Output / Explanation

When you run:

# Start the Django development server
python manage.py runserver

and open http://127.0.0.1:8000/ in your browser, Django uses the URL patterns in myproject/urls.py to match the empty path ''. This points to views.home in myapp/views.py.

The home function receives the request and returns an HttpResponse with the text: "Hello, Django!". That text is what you see rendered in the browser as a simple web page.

? Tips & Best Practices

  • Use virtual environments to manage Django projects and keep dependencies isolated.
  • Keep apps small and focused on one purpose to maintain a clean project structure.
  • Use Django’s built-in admin interface for quick data management and testing.
  • Always add new apps to INSTALLED_APPS in settings.py so Django can load them.
  • Separate project-level urls.py from app-level urls.py to avoid confusion.
  • Run database migrations (python manage.py makemigrations and python manage.py migrate) before using models.

? Try It Yourself

  • Create a Django project and app, and display a “Hello, World!” page using a view and URL mapping.
  • Add a second view (for example, about) and map it to a new URL like /about/.
  • Experiment with Django templates: create an HTML file and render it from a view instead of returning plain text.