商城-学习整理-基础-环境搭建(二)
发布人:shili8
发布时间:2025-02-05 00:22
阅读次数:0
**商城-学习整理-基础-环境搭建(二)**
在上一篇文章中,我们已经完成了基本的环境搭建,包括安装必要的依赖包、配置数据库连接等。在本篇文章中,我们将继续深入探讨环境搭建的其他方面。
###1.项目结构一个标准的商城项目应该具有清晰的目录结构。我们可以按照以下方式组织我们的代码:
bash商城/ |---- app.py # 应用程序入口文件|---- config/ # 配置文件夹| |---- database.py # 数据库配置| |---- settings.py # 应用程序设置|---- models/ # 模型定义文件夹| |---- user.py # 用户模型| |---- product.py #产品模型|---- views/ # 视图函数文件夹| |---- index.py # 首页视图函数| |---- detail.py #产品详情视图函数|---- templates/ # 模板文件夹| |---- base.html # 基础模板| |---- index.html # 首页模板|---- static/ # 静态资源文件夹| |---- css/ # CSS 文件夹| |---- js/ # JavaScript 文件夹
###2. 配置文件配置文件是应用程序的重要组成部分。我们需要在 `config` 文件夹中创建两个配置文件: `database.py` 和 `settings.py`。
#### database.py
# config/database.pyimport sqlite3class Database: def __init__(self): self.conn = sqlite3.connect('商城.db') self.cursor = self.conn.cursor() def create_table(self): # 创建用户表 self.cursor.execute(''' CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, username TEXT NOT NULL, password TEXT NOT NULL ) ''') # 创建产品表 self.cursor.execute(''' CREATE TABLE IF NOT EXISTS products ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, price REAL NOT NULL ) ''') def close(self): self.conn.commit() self.conn.close() # 实例化数据库对象db = Database()
#### settings.py
# config/settings.pyclass Settings: DEBUG = True # 是否开启调试模式 SECRET_KEY = 'secret_key' # 应用程序密钥 DATABASE_URL = 'sqlite:///商城.db' # 数据库连接 URL
###3. 模型定义模型是应用程序的核心组成部分。我们需要在 `models` 文件夹中创建两个模型: `User` 和 `Product`。
#### user.py
# models/user.pyclass User: def __init__(self, id, username, password): self.id = id self.username = username self.password = password @classmethod def get_by_username(cls, username): # 根据用户名获取用户对象 pass def save(self): #保存用户对象到数据库 pass
#### product.py
# models/product.pyclass Product: def __init__(self, id, name, price): self.id = id self.name = name self.price = price @classmethod def get_by_id(cls, id): # 根据 ID 获取产品对象 pass def save(self): #保存产品对象到数据库 pass
###4. 视图函数视图函数是应用程序的重要组成部分。我们需要在 `views` 文件夹中创建两个视图函数: `index` 和 `detail`。
#### index.py
# views/index.pyfrom flask import render_templatedef index(): # 首页视图函数 return render_template('index.html')
#### detail.py
# views/detail.pyfrom flask import render_templatedef detail(product_id): #产品详情视图函数 product = Product.get_by_id(product_id) return render_template('detail.html', product=product)
###5. 模板文件模板文件是应用程序的重要组成部分。我们需要在 `templates` 文件夹中创建两个模板: `index.html` 和 `detail.html`。
#### index.html
html<!-- templates/index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>首页</title> </head> <body> <h1>欢迎来到商城!</h1> <ul> {% for product in products %} <li>{{ product.name }} ({{ product.price }})</li> {% endfor %} </ul> </body> </html>
#### detail.html
html<!-- templates/detail.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>产品详情</title> </head> <body> <h1>{{ product.name }}</h1> <p>价格: {{ product.price }}</p> </body> </html>
以上就是本篇文章的内容。通过阅读这篇文章,希望你能够了解商城项目的基本结构和组成部分,以及如何搭建一个标准的商城环境。