Back to: Python Programming
Prerequisites
Before getting started with Django, you should have a basic understanding of Python programming language, HTML, and CSS.
You will also need to install Python and Django on your system. You can do this by following these steps:
- Download and install the latest version of Python from the official website: https://www.python.org/downloads/
- Open the terminal or command prompt and enter the following command to install Django using pip:
pip install django
Creating a Django project
Once you have installed Django, you can create a new Django project using the following command:
django-admin startproject myproject
This will create a new Django project with the name “myproject”. The project will contain a manage.py file, which is a command-line utility for interacting with the project.
Creating a Django app
A Django project is made up of one or more apps. Each app is a self-contained module that can be reused in other projects. You can create a new app using the following command:
python manage.py startapp myapp
This will create a new app with the name “myapp” in your project. The app will contain a models.py file, which is where you define your data models, a views.py file, which is where you define your views, and a templates folder, which is where you put your HTML templates.
Defining models
A model is a Python class that defines the structure of a database table. You can define your models in the models.py file of your app. Here is an example model:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=50)
pub_date = models.DateField()
In this example, we define a Book model with three fields: title, author, and pub_date.
Creating database tables
Once you have defined your models, you need to create database tables based on those models. You can do this using the following command:
python manage.py makemigrations
This command generates a migration file that describes the changes to the database schema. You can then apply the migration using the following command:
python manage.py migrate
This command creates the database tables based on your models.
Creating views
A view is a Python function that handles a web request and returns a web response. You can define your views in the views.py file of your app. Here is an example view:
from django.shortcuts import render
from .models import Book
def book_list_view(request):
books = Book.objects.all()
return render(request, 'book_list.html', {'books': books})
In this example, we define a view called book_list_view that fetches all books from the database and passes them to a template called book_list.html.
Creating templates
A template is an HTML file that defines the structure of a web page. You can create your templates in the templates folder my-template
under your app. Here is an example template:
{% extends 'base.html' %}
{% block content %}
<h1>Book list</h1>
<ul>
{% for book in books %}
<li>{{ book.title }} by {{ book.author }}</li>
{% endfor %}
</ul>
{% endblock %}
In this example, we define a template called book_list.html that extends a base template called base.html. The template displays a list of books using a for loop.
Configuring URLs
To make your views accessible from the web, you need to map them to URLs.
Create a URL pattern
URL patterns are used to map a URL path to a specific view. To create a URL pattern, you’ll create a new file called urls.py
within your app directory and define your URL pattern as a Python regular expression.
For example, here’s a simple URL pattern that maps the URL path /my-template/
to the book_list_view
view:
from django.urls import path
from .views import book_list_view
urlpatterns = [
path('my-template/', book_list_view, name='book_list'),
]
In this example, the path
function is used to define a URL pattern that matches the /my-template/
URL path and calls the book_list_view
view.
Include your URL patterns in your project
Once you’ve defined your URL patterns, you’ll need to include them in your project’s URL configuration. To do this, you’ll create a new file called urls.py
within your project directory and define your URL patterns using the include
function.
For example, here’s a simple project URL configuration that includes the myapp
app’s URL patterns:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
In this example, the path
function is used to define two URL patterns: one for the Django admin site, and one that includes the URL patterns for the myapp
app.
Test your URLs
Once you’ve configured your URLs and HTML template, you can test them by running your Django project and visiting the corresponding URLs in your web browser. For example, if you’re running your project on localhost
at port 8000
, you can visit http://localhost:8000/my-template/
to see the available booklist in the database.