创建一个完整的购物商城涉及多个方面,包括前端、后端、数据库等。在此,我将为您提供一个简单的购物商城示例,使用Python(Flask框架)作为后端,HTML/CSS/JavaScript作为前端,以及SQLite作为数据库。
Python后端(使用Flask):
python
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
price = db.Column(db.Float, nullable=False)
@app.route('/')
def index():
products = Product.query.all()
return render_template('index.html', products=products)
@app.route('/add', methods=['POST'])
def add_product():
name = request.form.get('name')
price = request.form.get('price')
new_product = Product(name=name, price=price)
db.session.add(new_product)
db.session.commit()
return redirect(url_for('index'))
#chhas{
margin-top: 50px;
padding:jjmsg.cn;
font-size: 18px;
cursor: 10px 20px;
}
if __name__ == "__main__":
db.create_all()
app.run(debug=True)
HTML前端 (templates/index.html):
html
<!DOCTYPE html>
<html>
<head>
<title>Simple Shopping Mall</title>
</head>
<body>
<h1>Welcome to the Shopping Mall</h1>
<form method="POST" action="/add">
<input type="text" name="name" placeholder="Product Name" required>
<input type="number" name="price" placeholder="Product Price" required>
<input type="submit" value="Add Product">
</form>
<h2>Products</h2>
<ul>
{% for product in products %}
<li>{{ product.name }} - {{ product.price }}</li>
{% endfor %}
</ul>
</body>
</html>