Python (Flask)rjdxjy.com
这里我们使用 Flask 框架来创建一个简单的 web 应用。
app.py:
python
from flask import Flask, render_template, request
app = Flask(name)
假设的商品列表
products = [
{“id”: 1, “name”: “Product 1”, “price”: 100},
{“id”: 2, “name”: “Product 2”, “price”: 200},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
@app.route(‘/cart’, methods=[‘POST’])
def cart():
product_id = request.form.get(‘product_id’)
# 这里只是简单地将产品ID添加到购物车(实际上需要更复杂的逻辑)
print(f"Added product {product_id} to cart")
return “Product added to cart”
if name == ‘main’:
app.run(debug=True)
templates/index.html:
html
Products
{% for product in products %}{{ product.name }} - Price: {{ product.price }}
Add to CartProductController.java:
java
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@RestController
public class ProductController {
private List<Product> products = Arrays.asList(
new Product(1, "Product 1", 100),
new Product(2, "Product 2", 200)
// ...
);
// 假设的 Product 类
static class Product {
int id;
String name;
double price;
// 构造器、getter、setter...
}
@GetMapping("/")
public List<Product> getProducts() {
return products;
}
// 这里应该有一个处理添加到购物车的 POST 请求的方法,但为了简洁省略了
}
JavaScript (Node.js + Express)
server.js:
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
let products = [
{id: 1, name: ‘Product 1’, price: 100},
{id: 2, name: ‘Product 2’, price: 200},
// …
];
app.get(‘/’, (req, res) => {
res.send( <h1>Products</h1> <form method="post" action="/cart"> ${products.map(product =>
${product.name} - Price:
p
r
o
d
u
c
t
.
p
r
i
c
e
<
/
p
>
<
b
u
t
t
o
n
t
y
p
e
=
"
s
u
b
m
i
t
"
n
a
m
e
=
"
p
r
o
d
u
c
t
i
d
"
v
a
l
u
e
=
"
{product.price}</p> <button type="submit" name="product_id" value="
product.price</p><buttontype="submit"name="productid"value="{product.id}">Add to Cart
).join('')} </form>
);
});
app.post(‘/cart’, (req, res) => {
const productId = req.body.product_id;
// 这里只是简单地将产品ID添加到购物车(实际上需要更复杂的逻辑)
console.log(Added product ${productId} to cart
);
res.send(‘Product added to cart’);
});
app.listen(port, () => {
console.log(App listening at http://localhost:${port}
);
});
注意:这个 Node.js 示例没有使用模板引擎,而是直接在服务器端拼接 HTML。在实际应用中,你可能会使用像 Pug由于生成一个完整的购物商城代码会相当长且复杂,我会为你提供一个非常简化的购物商城的概念框架,用几种常见的编程语言(Python, Java, JavaScript)来描述其核心部分。
Python (Flask)
这里我们使用 Flask 框架来创建一个简单的 web 应用。
app.py:
python
from flask import Flask, render_template, request
app = Flask(name)
假设的商品列表
products = [
{“id”: 1, “name”: “Product 1”, “price”: 100},
{“id”: 2, “name”: “Product 2”, “price”: 200},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
@app.route(‘/cart’, methods=[‘POST’])
def cart():
product_id = request.form.get(‘product_id’)
# 这里只是简单地将产品ID添加到购物车(实际上需要更复杂的逻辑)
print(f"Added product {product_id} to cart")
return “Product added to cart”
if name == ‘main’:
app.run(debug=True)
templates/index.html:
html
Products
{% for product in products %}{{ product.name }} - Price: {{ product.price }}
Add to CartProductController.java:
java
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@RestController
public class ProductController {
private List<Product> products = Arrays.asList(
new Product(1, "Product 1", 100),
new Product(2, "Product 2", 200)
// ...
);
// 假设的 Product 类
static class Product {
int id;
String name;
double price;
// 构造器、getter、setter...
}
@GetMapping("/")
public List<Product> getProducts() {
return products;
}
// 这里应该有一个处理添加到购物车的 POST 请求的方法,但为了简洁省略了
}
JavaScript (Node.js + Express)
server.js:
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
let products = [
{id: 1, name: ‘Product 1’, price: 100},
{id: 2, name: ‘Product 2’, price: 200},
// …
];
app.get(‘/’, (req, res) => {
res.send( <h1>Products</h1> <form method="post" action="/cart"> ${products.map(product =>
${product.name} - Price:
p
r
o
d
u
c
t
.
p
r
i
c
e
<
/
p
>
<
b
u
t
t
o
n
t
y
p
e
=
"
s
u
b
m
i
t
"
n
a
m
e
=
"
p
r
o
d
u
c
t
i
d
"
v
a
l
u
e
=
"
{product.price}</p> <button type="submit" name="product_id" value="
product.price</p><buttontype="submit"name="productid"value="{product.id}">Add to Cart
).join('')} </form>
);
});
app.post(‘/cart’, (req, res) => {
const productId = req.body.product_id;
// 这里只是简单地将产品ID添加到购物车(实际上需要更复杂的逻辑)
console.log(Added product ${productId} to cart
);
res.send(‘Product added to cart’);
});
app.listen(port, () => {
console.log(App listening at http://localhost:${port}
);
});
注意:这个 Node.js 示例没有使用模板引擎,而是直接在服务器端拼接 HTML。在实际应用中,你可能会使用像 Pug