Python (Flask)621112.com
python
from flask import Flask, render_template, request, redirect, url_for
app = Flask(name)
假设的商品数据
products = [
{‘id’: 1, ‘name’: ‘商品1’, ‘price’: 100},
{‘id’: 2, ‘name’: ‘商品2’, ‘price’: 200},
# … 更多商品
]
购物车(这里简化为全局变量,实际中应存储在数据库或会话中)
cart = []
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
@app.route(‘/add_to_cart/int:product_id’)
def add_to_cart(product_id):
for product in products:
if product[‘id’] == product_id:
cart.append(product)
break
return redirect(url_for(‘cart’))
@app.route(‘/cart’)
def cart():
return render_template(‘cart.html’, cart=cart)
… 其他路由,如结账、登录、注册等
if name == ‘main’:
app.run(debug=True)
HTML模板(例如 templates/index.html 和 templates/cart.html)需要您自己创建。
JavaScript (Node.js + Express)
Node.js和Express的代码结构与Flask类似,但您需要使用JavaScript来编写。以下是一个简化的Express应用示例:
javascript
const express = require(‘express’);
const app = express();
const path = require(‘path’);
// 假设的商品数据(这里简化为硬编码,实际中应从数据库获取)
let products = [
{ id: 1, name: ‘商品1’, price: 100 },
{ id: 2, name: ‘商品2’, price: 200 },
// … 更多商品
];
// 购物车(这里简化为内存中的数组,实际中应存储在数据库或会话中)
let cart = [];
app.set(‘views’, path.join(__dirname, ‘views’));
app.set(‘view engine’, ‘pug’); // 假设使用Pug模板引擎
app.get(‘/’, (req, res) => {
res.render(‘index’, { products: products });
});
app.get(‘/add_to_cart/:product_id’, (req, res) => {
const productId = parseInt(req.params.product_id);
const product = products.find(p => p.id === productId);
if (product) {
cart.push(product);
}
res.redirect(‘/cart’);
});
app.get(‘/cart’, (req, res) => {
res.render(‘cart’, { cart: cart });
});
// … 其他路由
app.listen(3000, () => console.log(‘Server started on port 3000’));
Pug模板(或您选择的任何其他模板引擎)需要您自己创建。
请注意,这些示例仅用于说明如何开始构建购物商城的后端部分。前端部分(包括用户界面、AJAX请求等)以及数据库集成、安全性、错误处理等更多功能都需要您自己实现。由于直接在一个回答中提供完整的购物商城代码是不切实际的(考虑到代码的长度和复杂性),我将为您提供一个简化的购物商城代码示例,分别用Python(使用Flask框架)和JavaScript(使用Node.js和Express框架)来实现。请注意,这些示例仅作为起点,真正的商城应用需要更多的功能和安全性考虑。
Python (Flask)
python
from flask import Flask, render_template, request, redirect, url_for
app = Flask(name)
假设的商品数据
products = [
{‘id’: 1, ‘name’: ‘商品1’, ‘price’: 100},
{‘id’: 2, ‘name’: ‘商品2’, ‘price’: 200},
# … 更多商品
]
购物车(这里简化为全局变量,实际中应存储在数据库或会话中)
cart = []
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
@app.route(‘/add_to_cart/int:product_id’)
def add_to_cart(product_id):
for product in products:
if product[‘id’] == product_id:
cart.append(product)
break
return redirect(url_for(‘cart’))
@app.route(‘/cart’)
def cart():
return render_template(‘cart.html’, cart=cart)
… 其他路由,如结账、登录、注册等
if name == ‘main’:
app.run(debug=True)
HTML模板(例如 templates/index.html 和 templates/cart.html)需要您自己创建。
JavaScript (Node.js + Express)
Node.js和Express的代码结构与Flask类似,但您需要使用JavaScript来编写。以下是一个简化的Express应用示例:
javascript
const express = require(‘express’);
const app = express();
const path = require(‘path’);
// 假设的商品数据(这里简化为硬编码,实际中应从数据库获取)
let products = [
{ id: 1, name: ‘商品1’, price: 100 },
{ id: 2, name: ‘商品2’, price: 200 },
// … 更多商品
];
// 购物车(这里简化为内存中的数组,实际中应存储在数据库或会话中)
let cart = [];
app.set(‘views’, path.join(__dirname, ‘views’));
app.set(‘view engine’, ‘pug’); // 假设使用Pug模板引擎
app.get(‘/’, (req, res) => {
res.render(‘index’, { products: products });
});
app.get(‘/add_to_cart/:product_id’, (req, res) => {
const productId = parseInt(req.params.product_id);
const product = products.find(p => p.id === productId);
if (product) {
cart.push(product);
}
res.redirect(‘/cart’);
});
app.get(‘/cart’, (req, res) => {
res.render(‘cart’, { cart: cart });
});
// … 其他路由
app.listen(3000, () => console.log(‘Server started on port 3000’));
Pug模板(或您选择的任何其他模板引擎)需要您自己创建。
请注意,这些示例仅用于说明如何开始构建购物商城的后端部分。前端部分(包括用户界面、AJAX请求等)以及数据库集成、安全性、错误处理等更多功能都需要您自己实现。