Step 1: Install Python
Before we can start working with Django, we need to install Python on our system. You can download and install Python from the official website: https://www.python.org/downloads/
Step 2: Install Django
After installing Python, you can install Django using pip, which is the package installer for Python. Open the command prompt (Windows) or terminal (macOS/Linux) and run the following command:
pip install Django
This will install the latest version of Django on your system.
Step 3: Create a new Django project
Once Django is installed, we can create a new Django project. Open the command prompt or terminal and navigate to the directory where you want to create your project. Then run the following command:
django-admin startproject myproject
This will create a new Django project named "myproject" in the current directory.
Step 4: Create a new Django app
A Django project consists of one or more apps, which are self-contained modules that perform specific tasks. To create a new app, navigate to the project directory (the one containing manage.py
) and run the following command:
python manage.py startapp myapp
This will create a new Django app named "myapp" in the myproject
directory.
Step 5: Define models
In Django, models are used to define the structure of your data. Open the models.py
file in the myapp
directory and define your models. For example:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=50)
published_date = models.DateField()
def __str__(self):
return self.title
This defines a Book
model with three fields: title
, author
, and published_date
.
Step 6: Create database tables
Once you have defined your models, you need to create database tables based on them. Run the following commands:
python manage.py makemigrations
python manage.py migrate
The first command generates a migration file based on your model changes, and the second command applies those changes to your database.
Step 7: Create views
In Django, views are used to handle HTTP requests and generate responses. Open the views.py
file in the myapp
directory and define your views. For example:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Book
def index(request):
books = Book.objects.all()
context = {'books': books}
return render(request, 'index.html', context)
def detail(request, book_id):
book = Book.objects.get(id=book_id)
context = {'book': book}
return render(request, 'detail.html', context)
This defines two views: index
and detail
. The index
view retrieves all books from the database and renders them using the index.html
template. The detail
view retrieves a specific book based on its ID and renders it using the detail.html
template.
Step 8: Create templates
Templates are used to generate HTML pages that are sent as responses to HTTP requests. Create a new directory called templates
in the myapp
directory, and then create two new files: index.html
and detail.html
. For example:
index.html
:
{% extends 'base.html' %}
{% block content %}
<h1>Books</h1>
<ul>
{% for book in books %}
TAGS