Table of Contents
- Introduction
- Prerequisites
- Setting up Django
- Creating a Django Project
- Creating a Django App
- Defining Models
- Creating Views
- Setting up URLs
- Creating Templates
- Running the Application
- Conclusion
Introduction
Welcome to this tutorial on Python for web development using the Django framework. By the end of this tutorial, you will have built a basic Django application and gained an understanding of how to create web applications with Python.
Prerequisites
Before starting this tutorial, make sure you have the following:
- Basic knowledge of Python programming language
- Python installed on your system
- Familiarity with HTML, CSS, and web development concepts
Setting up Django
To get started, we need to install Django. Open your terminal or command prompt and run the following command:
pip install django
This will install the latest version of Django on your system.
Creating a Django Project
- Open your terminal or command prompt.
- Navigate to the directory where you want to create your Django project.
-
Run the following command to create a new Django project:
django-admin startproject myproject
This will create a new directory named
myproject
with the basic structure of a Django project.
Creating a Django App
-
Navigate to the project directory:
cd myproject
-
Run the following command to create a new Django app within the project:
python manage.py startapp myapp
This will create a new directory named
myapp
inside themyproject
directory.
Defining Models
- Open the
myapp/models.py
file in your preferred text editor. -
Define your models using Python classes. For example:
from django.db import models class Product(models.Model): name = models.CharField(max_length=100) price = models.DecimalField(max_digits=5, decimal_places=2) description = models.TextField()
This example creates a
Product
model with three fields:name
,price
, anddescription
. - Save the
models.py
file.
Creating Views
- Open the
myapp/views.py
file. -
Import the necessary modules:
from django.shortcuts import render from django.http import HttpResponse from .models import Product
-
Define your views using Python functions:
def product_list(request): products = Product.objects.all() return render(request, 'myapp/product_list.html', {'products': products}) def product_detail(request, pk): product = Product.objects.get(pk=pk) return render(request, 'myapp/product_detail.html', {'product': product})
These views retrieve data from the
Product
model and pass it to the corresponding template. - Save the
views.py
file.
Setting up URLs
- Open the
myproject/urls.py
file. -
Import the necessary modules:
from django.urls import path from myapp import views
-
Define your URL patterns:
urlpatterns = [ path('', views.product_list, name='product_list'), path('product/<int:pk>/', views.product_detail, name='product_detail'), ]
These patterns map URLs to the corresponding views.
- Save the
urls.py
file.
Creating Templates
- Create a new directory named
templates
inside themyapp
directory. - Inside the
templates
directory, create a new directory namedmyapp
. -
Create two HTML files:
-
product_list.html
:<h1>Product List</h1>
-
product_detail.html
:<h1></h1> <p></p> <p></p>
These templates define the structure and layout of the web pages.
-
Running the Application
- Open your terminal or command prompt.
- Navigate to the project directory (
myproject
). -
Run the following command to start the development server:
python manage.py runserver
This will start the development server on
http://localhost:8000/
. - Open your web browser and visit
http://localhost:8000/
to see the product list.
Conclusion
In this tutorial, you learned how to build a Django application for web development using Python. You started by setting up Django, creating a project and app, defining models, creating views, setting up URLs, and creating templates. Finally, you ran the Django development server and accessed your web application.
You can now continue to enhance your Django application by adding more models, views, templates, and functionalities. Explore the Django documentation and other resources to further expand your web development skills with Python.
Now you are ready to pursue more advanced topics in web development with Django and Python!