可在下方:领取源代码
制作一个完整的购物网站涉及多个方面,包括前端页面设计、后端逻辑实现、数据库设计等。
1. 项目结构
首先,我们需要创建一个项目目录,并设置项目结构:
shopping_site/
│
├── app.py
├── templates/
│ ├── index.html
│ ├── product.html
│ └── cart.html
├── static/
│ ├── css/
│ │ └── style.css
│ └── js/
│ └── script.js
└── database.db
2. 安装依赖
我们将使用Flask框架来构建这个购物网站。首先,我们需要安装Flask:
pip install Flask
3. 创建Flask应用
在app.py
文件中,我们创建一个Flask应用,并设置一些基本配置:
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/product/<int:product_id>')
def product(product_id):
# 这里我们应该从数据库中获取产品信息
product = {
'id': product_id,
'name': 'Product Name',
'price': 100
}
return render_template('product.html', product=product)
@app.route('/cart', methods=['GET', 'POST'])
def cart():
if request.method == 'POST':
product_id = request.form['product_id']
# 这里我们应该将产品添加到购物车中
return redirect(url_for('cart'))
# 这里我们应该从数据库中获取购物车信息
cart_items = []
return render_template('cart.html', cart_items=cart_items)
if __name__ == '__main__':
app.run(debug=True)
4. 创建前端页面
在templates
目录中,我们创建三个HTML文件:index.html
、product.html
和cart.html
。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Site</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div class="container">
<h1>Shopping Site</h1>
<ul>
<li><a href="{{ url_for('product', product_id=1) }}">Product 1</a></li>
<li><a href="{{ url_for('product', product_id=2) }}">Product 2</a></li>
</ul>
<a href="{{ url_for('cart') }}">View Cart</a>
</div>
</body>
</html>
product.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div class="container">
<h1>{{ product.name }}</h1>
<p>Price: ${{ product.price }}</p>
<form action="{{ url_for('cart') }}" method="POST">
<input type="hidden" name="product_id" value="{{ product.id }}">
<button type="submit">Add to Cart</button>
</form>
</div>
</body>
</html>
cart.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cart</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div class="container">
<h1>Shopping Cart</h1>
<ul>
{% for item in cart_items %}
<li>{{ item.name }} - ${{ item.price }}</li>
{% endfor %}
</ul>
</div>
</body>
</html>
5. 创建静态文件
在static
目录中,我们创建一些CSS和JS文件来美化和增强我们的网站。
style.css
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
}
ul {
list-style-type: none;
padding: 0;
}
ul li {
padding: 10px;
border-bottom: 1px solid #ddd;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
script.js
// 这里可以添加一些JavaScript代码来增强网站的功能
6. 运行应用
最后,我们在命令行中运行app.py
文件来启动Flask应用:
python app.py
现在,我们可以通过访问http://127.0.0.1:5000
来查看我们的购物网站。
这个示例只是一个简单的购物网站,实际的购物网站可能需要更多的功能和复杂的逻辑。但是,这个示例应该可以帮助你开始构建你自己的购物网站。
最后,如果你也想自学Python,可以关注我。
我还整理出了一套系统的学习路线,这套资料涵盖了诸多学习内容: 【点击这里】领取!
包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!开发工具,基础视频教程,项目实战源码,51本电子书籍,100道练习题等。相信可以帮助大家在最短的时间内,能达到事半功倍效果,用来复习也是非常不错的。