Python for Web Development: Building a Django Application

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setting up Django
  4. Creating a Django Project
  5. Creating a Django App
  6. Defining Models
  7. Creating Views
  8. Setting up URLs
  9. Creating Templates
  10. Running the Application
  11. 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

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your Django project.
  3. 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

  1. Navigate to the project directory:

    cd myproject
    
  2. 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 the myproject directory.

Defining Models

  1. Open the myapp/models.py file in your preferred text editor.
  2. 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, and description.

  3. Save the models.py file.

Creating Views

  1. Open the myapp/views.py file.
  2. Import the necessary modules:

    from django.shortcuts import render
    from django.http import HttpResponse
    from .models import Product
    
  3. 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.

  4. Save the views.py file.

Setting up URLs

  1. Open the myproject/urls.py file.
  2. Import the necessary modules:

    from django.urls import path
    from myapp import views
    
  3. 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.

  4. Save the urls.py file.

Creating Templates

  1. Create a new directory named templates inside the myapp directory.
  2. Inside the templates directory, create a new directory named myapp.
  3. 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

  1. Open your terminal or command prompt.
  2. Navigate to the project directory (myproject).
  3. Run the following command to start the development server:

    python manage.py runserver
    

    This will start the development server on http://localhost:8000/.

  4. 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!