- 后端(Python + Flask)huaihaihotelshanghai.cn
app.py:
python
from flask import Flask, render_template, request, jsonify
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(‘/add_to_cart’, methods=[‘POST’])
def add_to_cart():
# 这里只是简单模拟,实际应处理购物车逻辑
product_id = request.json.get(‘product_id’)
# 假设添加成功,返回成功信息
return jsonify({‘status’: ‘success’, ‘message’: f’Product {product_id} added to cart’})
if name == ‘main’:
app.run(debug=True)
2. 前端(HTML + CSS + JavaScript)
templates/index.html:
html
{{ product.name }}
Price: {{ product.price }}
Add to Cart<script>
function addToCart(productId) {
fetch('/add_to_cart', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({product_id: productId})
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
alert(data.message);
}
})
.catch(error => console.error('Error:', error));
}
</script>
3. 其他语言 Java (Spring Boot): Spring Boot 是 Java 的一个流行框架,用于构建 RESTful Web 服务。 Node.js (Express): Express 是一个基于 Node.js 的 web 开发框架,可以快速搭建 web 应用程序。 Ruby (Ruby on Rails): Ruby on Rails 是一个用于构建 web 应用程序的完整框架,遵循 MVC 模式。 PHP (Laravel/Symfony): Laravel 和 Symfony 是 PHP 的流行框架,提供了丰富的功能和工具来构建复杂的 web 应用程序。 请注意,以上示例仅用于演示目的,并未包含完整的购物车逻辑、用户认证、数据库交互等关键功能。在实际开发中,你需要根据具体需求进行设计和实现。由于编写一个完整的购物商城代码是一个相当复杂的任务,涉及多个页面、数据库交互、后端逻辑和前端展示,所以在这里我只能为你提供一个非常简化的示例,分别使用Python(Flask框架)、JavaScript(前端逻辑)、HTML和CSS来展示基本概念。
- 后端(Python + Flask)
app.py:
python
from flask import Flask, render_template, request, jsonify
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(‘/add_to_cart’, methods=[‘POST’])
def add_to_cart():
# 这里只是简单模拟,实际应处理购物车逻辑
product_id = request.json.get(‘product_id’)
# 假设添加成功,返回成功信息
return jsonify({‘status’: ‘success’, ‘message’: f’Product {product_id} added to cart’})
if name == ‘main’:
app.run(debug=True)
2. 前端(HTML + CSS + JavaScript)
templates/index.html:
html
{{ product.name }}
Price: {{ product.price }}
Add to Cart<script>
function addToCart(productId) {
fetch('/add_to_cart', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({product_id: productId})
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
alert(data.message);
}
})
.catch(error => console.error('Error:', error));
}
</script>
3. 其他语言 Java (Spring Boot): Spring Boot 是 Java 的一个流行框架,用于构建 RESTful Web 服务。 Node.js (Express): Express 是一个基于 Node.js 的 web 开发框架,可以快速搭建 web 应用程序。 Ruby (Ruby on Rails): Ruby on Rails 是一个用于构建 web 应用程序的完整框架,遵循 MVC 模式。 PHP (Laravel/Symfony): Laravel 和 Symfony 是 PHP 的流行框架,提供了丰富的功能和工具来构建复杂的 web 应用程序。 请注意,以上示例仅用于演示目的,并未包含完整的购物车逻辑、用户认证、数据库交互等关键功能。在实际开发中,你需要根据具体需求进行设计和实现。