Home  >  Article  >  Backend Development  >  Python and Django programming practice: building an online mall

Python and Django programming practice: building an online mall

WBOY
WBOYOriginal
2023-06-22 19:21:171241browse

In today’s digital age, more business transactions are moving online. Therefore, in order to compete successfully, many businesses need a powerful online store. In this article, we will demonstrate how to build an online store using Python and the Django framework.

Python and Django are a very powerful pair of tools that excel in developing modern web applications. Python has easy-to-learn and understand syntax, rich libraries and powerful development tools. Django is a highly customizable framework that introduces cutting-edge development practices. It includes many functions such as ORM, template engine, form processing and management backend. Using Python and Django, building a powerful online store is relatively simple.

Below, we will briefly introduce the main steps to implement an online mall under Python and Django, including establishing projects and applications, designing data models, and implementing view functions.

1. Create Django projects and applications

First you need to install Python and Django. After the installation is complete, open a terminal and create a Django project. To do this, enter the following command:

django-admin startproject my_store_project

This will create a project called 'my_store_project'. Next, we will create an application in the project to provide services for the mall page. To create an application, type the following command:

python manage.py startapp my_store_app

This will create an application folder called 'my_store_app' in the project.

2. Design the data model

Next, we need to design the data model. We will create two models, Products and Customers.

Open the 'models.py' file in the 'my_store_app' folder and add the following code in it:

from django.db import models

class Product(models. Model):
name = models.CharField(max_length=255)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)

class Customer(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField()
address = models.TextField()

These models define The data and attributes our mall needs. In the product model, we define the name, description, and price of the product. In the customer model, we define the customer's name, email address, and address information. Additionally, each model has its own primary key that can be used as an identifier for data access and manipulation.

3. Implement the view

Next, we will create a view to provide services for the website. In this project we will use Tablet, a front-end framework written in Django using Python. It allows us to create beautiful web user interfaces from code.

To use Tablet, please install it first. Open a terminal and enter the following command:

pip install django-tables2

Create a 'views.py' file in the 'my_store_app' folder and add the following code:

from django.shortcuts import render
from .models import Product

def product_list(request):
products = Product.objects.all()
return render(request, 'my_store_app/ product_list.html', {'products': products})

In this code block, we define a view function named 'product_list', which uses the ' Product' model. The function queries the database and passes all products to the template. In this way, in the function, we can present a list of products and present all product details to the web page user.

Next, create a 'urls.py' file in the 'my_store_app' folder and add the following code:

from django.urls import path
from . import views

urlpatterns = [
path('', views.product_list, name='product_list'),
]

In this code block, we define a file named ' product_list' view function, which will render the list of all products stored in the database on the requested URL.

4. Create HTML template

Finally, we need to create an HTML template for the mall. Create a folder called "templates" inside the 'my_store_app' folder. In this folder, create an HTML template called " product_list.html ". In it, we will use a tablet to design the page.

Create a new folder "include" in the "templates" folder and create files named "header.html" and "footer.html". In it, we will define all the standard page elements such as logo, header, footer, navigation bar, etc.

Here is a simple file example:

aba7b36f87decd50b18c7e3e3c150106
100db36a723c770d327fc0aef2ce13b1

<head>
    <title>我的商城</title>
</head>
<body>
    {% include "my_store_app/header.html" %}
    
    {% for product in products %}
        <h2>{{ product.name }}</h2>
        <p>{{ product.description }}</p>
        <p>{{ product.price }}</p>
    {% endfor %}
    
    {% include "my_store_app/footer.html" %}
</body>

73a6ac4ed44ffec12cee46588e518a5e

In this code snippet, we first reference all standard page elements, and then use the tablet to loop through the product list stored in the database. Finally, we render the footer and page elements.

Summary

In the article, we learned how to create an online mall through code using Python and the Django framework. We covered several important steps, including creating Django projects and applications, designing data models, implementing view functions, and creating HTML templates. This will help developers understand how to use these powerful tools to build highly customizable online stores.

The above is the detailed content of Python and Django programming practice: building an online mall. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn