用python实现网上书店
时间: 2023-07-12 14:12:36 浏览: 260
好的,以下是一个简单的网上书店的实现思路:
1. 数据库设计:创建书籍、用户、订单等表,定义表的字段及数据类型。
2. 界面设计:使用前端技术(如HTML、CSS、JavaScript)创建网站首页、书籍列表、购物车、订单等界面。
3. 编写代码:使用Python编写后端程序,实现用户注册、登录、书籍管理、购物车管理、订单管理等功能。
4. 测试:对系统进行测试,检查是否存在数据错误和逻辑错误。
5. 上线运行:将系统部署到服务器上,让用户进行使用。
具体实现可以参考以下代码:
```python
# 引入必要的库
from flask import Flask, render_template, request, session, redirect, url_for
import pymysql
# 创建Flask实例
app = Flask(__name__)
# 数据库连接配置
conn = pymysql.connect(
host='localhost',
user='root',
password='123456',
database='bookstore',
charset='utf8mb4'
)
# 首页路由
@app.route('/')
def index():
# 查询所有的书籍信息
cursor = conn.cursor()
cursor.execute('SELECT * FROM books')
books = cursor.fetchall()
cursor.close()
return render_template('index.html', books=books)
# 注册页面路由
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
# 获取用户输入的注册信息
username = request.form.get('username')
password = request.form.get('password')
email = request.form.get('email')
# 插入用户信息到数据库
cursor = conn.cursor()
cursor.execute('INSERT INTO users(username, password, email) VALUES(%s, %s, %s)', (username, password, email))
conn.commit()
cursor.close()
# 跳转到登录页面
return redirect(url_for('login'))
else:
return render_template('register.html')
# 登录页面路由
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# 获取用户输入的登录信息
username = request.form.get('username')
password = request.form.get('password')
# 查询用户信息
cursor = conn.cursor()
cursor.execute('SELECT * FROM users WHERE username=%s AND password=%s', (username, password))
user = cursor.fetchone()
cursor.close()
if user:
# 登录成功,保存用户信息到session中,并跳转到首页
session['user_id'] = user[0]
session['username'] = user[1]
return redirect(url_for('index'))
else:
# 登录失败,返回登录页面
return render_template('login.html', message='用户名或密码错误')
else:
return render_template('login.html')
# 书籍详情页面路由
@app.route('/book/<int:id>')
def book_detail(id):
# 查询指定id的书籍信息
cursor = conn.cursor()
cursor.execute('SELECT * FROM books WHERE id=%s', (id,))
book = cursor.fetchone()
cursor.close()
return render_template('book_detail.html', book=book)
# 添加购物车路由
@app.route('/add_to_cart', methods=['POST'])
def add_to_cart():
# 获取用户选择的书籍id和数量
book_id = request.form.get('book_id')
quantity = request.form.get('quantity')
# 保存购物车信息到session中
if 'cart' not in session:
session['cart'] = {}
cart = session['cart']
if book_id in cart:
cart[book_id] += int(quantity)
else:
cart[book_id] = int(quantity)
session['cart'] = cart
# 跳转到购物车页面
return redirect(url_for('cart'))
# 购物车页面路由
@app.route('/cart')
def cart():
# 获取购物车信息
cart = session.get('cart', {})
book_info = []
total_price = 0
# 查询购物车中的书籍信息
for book_id, quantity in cart.items():
cursor = conn.cursor()
cursor.execute('SELECT * FROM books WHERE id=%s', (book_id,))
book = cursor.fetchone()
cursor.close()
book_info.append({
'id': book_id,
'title': book[1],
'price': book[3],
'quantity': quantity,
'subtotal': book[3] * quantity
})
total_price += book[3] * quantity
return render_template('cart.html', book_info=book_info, total_price=total_price)
# 提交订单路由
@app.route('/submit_order', methods=['POST'])
def submit_order():
# 获取用户选择的收货地址和联系人
address = request.form.get('address')
contact = request.form.get('contact')
# 获取购物车信息
cart = session.get('cart', {})
# 生成订单
cursor = conn.cursor()
cursor.execute('INSERT INTO orders(user_id, address, contact) VALUES(%s, %s, %s)', (session['user_id'], address, contact))
order_id = cursor.lastrowid
for book_id, quantity in cart.items():
cursor.execute('INSERT INTO order_items(order_id, book_id, quantity) VALUES(%s, %s, %s)', (order_id, book_id, quantity))
conn.commit()
cursor.close()
# 清空购物车
session.pop('cart', None)
# 跳转到订单详情页面
return redirect(url_for('order_detail', id=order_id))
# 订单详情页面路由
@app.route('/order/<int:id>')
def order_detail(id):
# 查询订单信息
cursor = conn.cursor()
cursor.execute('SELECT * FROM orders WHERE id=%s AND user_id=%s', (id, session['user_id']))
order = cursor.fetchone()
if not order:
return '无效的订单'
cursor.execute('SELECT * FROM order_items WHERE order_id=%s', (id,))
order_items = cursor.fetchall()
cursor.close()
# 查询书籍信息
book_info = []
total_price = 0
for item in order_items:
cursor = conn.cursor()
cursor.execute('SELECT * FROM books WHERE id=%s', (item[2],))
book = cursor.fetchone()
cursor.close()
book_info.append({
'id': item[2],
'title': book[1],
'price': book[3],
'quantity': item[3],
'subtotal': book[3] * item[3]
})
total_price += book[3] * item[3]
return render_template('order_detail.html', order=order, book_info=book_info, total_price=total_price)
# 退出登录路由
@app.route('/logout')
def logout():
# 清除session中的用户信息
session.pop('user_id', None)
session.pop('username', None)
return redirect(url_for('index'))
# 启动Flask应用
app.secret_key = '123456'
if __name__ == '__main__':
app.run()
```
需要注意的是,网上书店的实现需要考虑到数据的安全性和稳定性,尤其是涉及到用户信息和订单信息等敏感信息时,要做好数据加密和备份。
阅读全文