Django Tutorial

Learn the basics of Django, a powerful web framework for Python.

Introduction to Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It’s built by experienced developers and takes care of much of the hassle of web development, allowing you to focus on writing your app without reinventing the wheel.

Prerequisites

Before diving into Django, make sure you have a basic understanding of the following:

Installing Django

To install Django, you can use pip, Python's package installer. Run the following command in your terminal:

pip install django

After installation, verify the version of Django installed:

django-admin --version

Creating a "Hello World" App

Let's create a simple Django project and app to display "Hello World" on the webpage.

Step 1: Create a Django Project

django-admin startproject myproject

Step 2: Create an App

cd myproject
python manage.py startapp hello

Step 3: Configure the App

Add your app to the INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    ...
    'hello',
]

Step 4: Create a View

In views.py, add a view to display "Hello World":

from django.http import HttpResponse

def hello_world(request):
    return HttpResponse("Hello, World!")

Step 5: Map the URL

In urls.py, map the URL to the view:

from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.hello_world),
]