创建一个完整的购物商城代码是一个复杂且庞大的任务,涉及到前端、后端、数据库等多个方面。在这里,我可以为您提供一些基本框架和思路,但请注意,这些只是起点,您需要根据自己的需求进一步完善和扩展。
1. Python + Flask (后端部分)
python
from flask import Flask, request, render_template
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)
#chhas{
margin-top: 50px;
padding:njwancheng.cn;
font-size: 18px;
cursor: 10px 20px;
}
@app.route('/add_to_cart/<int:product_id>', methods=['POST'])
def add_to_cart(product_id):
# 这里应该是将商品添加到购物车的逻辑
# 可以使用session或数据库来存储购物车信息
return "Product added to cart"
if __name__ == '__main__':
app.run(debug=True)
2. HTML + JavaScript (前端部分,index.html)
html
<!DOCTYPE html>
<html>
<head>
<title>Shopping Mall</title>
</head>
<body>
<h1>Welcome to the Shopping Mall</h1>
<ul>
{% for product in products %}
<li>
{{ product.name }} - ${{ product.price }}
<button οnclick="addToCart({{ product.id }})">Add to Cart</button>
</li>
{% endfor %}
</ul>
<script>
function addToCart(productId) {
fetch(`/add_to_cart/${productId}`, {method: 'POST'})
.then(response => response.text())
.then(data => alert(data));
}
</script>
</body>
</html>