在这个数字化时代,拥有一个个人博客或在线商城可以让你在互联网上展示自己的才华,或是开展电子商务。而Flask,作为Python中最受欢迎的Web框架之一,能够帮助你轻松实现这些目标。本文将带你深入了解Flask,并展示如何用它来搭建个人博客和在线商城。
一、Flask简介
Flask是一个轻量级的Web应用框架,由Armin Ronacher在2010年开发。它遵循WSGI协议,并以Python语言编写。Flask的核心只包含Web服务器网关接口(WSGI)的适配器和一些基本的模板渲染功能,因此它非常灵活,易于扩展。
1.1 Flask的特点
- 轻量级:Flask不包含数据库抽象层、表单验证、文件上传等功能,开发者可以根据自己的需求选择合适的第三方库进行扩展。
- 灵活:Flask允许开发者自定义路由、模板、错误处理等,满足个性化需求。
- 社区活跃:Flask拥有庞大的社区,为开发者提供了丰富的资源和帮助。
二、搭建个人博客
个人博客是展示个人观点、经验和知识的平台。下面我们将使用Flask搭建一个简单的个人博客。
2.1 准备工作
- 安装Python和pip
- 安装Flask框架:
pip install flask
2.2 创建项目结构
blog/
│
├── app.py
├── templates/
│ ├── base.html
│ └── index.html
└── static/
└── css/
2.3 编写代码
2.3.1 app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
2.3.2 templates/base.html
<!DOCTYPE html>
<html>
<head>
<title>个人博客</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<header>
<h1>个人博客</h1>
</header>
<main>
{% block content %}
{% endblock %}
</main>
<footer>
<p>版权所有 © 2021 个人博客</p>
</footer>
</body>
</html>
2.3.3 templates/index.html
{% extends 'base.html' %}
{% block content %}
<h2>欢迎来到我的博客</h2>
<p>这里是个人博客的首页,你可以在这里发布文章、分享经验。</p>
{% endblock %}
2.3.4 static/css/style.css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
padding: 10px 0;
text-align: center;
}
main {
padding: 20px;
}
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 10px 0;
}
2.4 运行项目
在终端中执行以下命令启动服务器:
python app.py
然后在浏览器中访问http://127.0.0.1:5000/,即可看到个人博客的首页。
三、搭建在线商城
在线商城是电子商务的重要组成部分。下面我们将使用Flask搭建一个简单的在线商城。
3.1 准备工作
- 安装Flask框架:
pip install flask
3.2 创建项目结构
online_mall/
│
├── app.py
├── templates/
│ ├── base.html
│ ├── index.html
│ ├── product.html
│ └── cart.html
└── static/
└── css/
3.3 编写代码
3.3.1 app.py
from flask import Flask, render_template, request, redirect, url_for, session
app = Flask(__name__)
app.secret_key = 'your_secret_key'
# 模拟商品数据
products = [
{'id': 1, 'name': '苹果', 'price': 10},
{'id': 2, 'name': '香蕉', 'price': 5},
{'id': 3, 'name': '橘子', 'price': 8}
]
@app.route('/')
def index():
return render_template('index.html', products=products)
@app.route('/add_to_cart/<int:product_id>')
def add_to_cart(product_id):
if 'cart' not in session:
session['cart'] = []
session['cart'].append(product_id)
return redirect(url_for('index'))
@app.route('/cart')
def cart():
cart_items = [product for product in products if product['id'] in session['cart']]
return render_template('cart.html', cart_items=cart_items)
if __name__ == '__main__':
app.run(debug=True)
3.3.2 templates/base.html
<!DOCTYPE html>
<html>
<head>
<title>在线商城</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<header>
<h1>在线商城</h1>
</header>
<main>
{% block content %}
{% endblock %}
</main>
<footer>
<p>版权所有 © 2021 在线商城</p>
</footer>
</body>
</html>
3.3.3 templates/index.html
{% extends 'base.html' %}
{% block content %}
<h2>欢迎来到在线商城</h2>
<div class="products">
{% for product in products %}
<div class="product">
<h3>{{ product.name }}</h3>
<p>价格:{{ product.price }}</p>
<a href="{{ url_for('add_to_cart', product_id=product.id) }}">加入购物车</a>
</div>
{% endfor %}
</div>
{% endblock %}
3.3.4 templates/cart.html
{% extends 'base.html' %}
{% block content %}
<h2>购物车</h2>
<div class="cart">
{% for cart_item in cart_items %}
<div class="cart-item">
<h3>{{ cart_item.name }}</h3>
<p>价格:{{ cart_item.price }}</p>
</div>
{% endfor %}
</div>
<a href="{{ url_for('index') }}">继续购物</a>
{% endblock %}
3.3.5 static/css/style.css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
padding: 10px 0;
text-align: center;
}
main {
padding: 20px;
}
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 10px 0;
}
.products {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.product {
border: 1px solid #ccc;
margin: 10px;
padding: 10px;
width: 200px;
}
.cart {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.cart-item {
border: 1px solid #ccc;
margin: 10px;
padding: 10px;
width: 200px;
}
3.4 运行项目
在终端中执行以下命令启动服务器:
python app.py
然后在浏览器中访问http://127.0.0.1:5000/,即可看到在线商城的首页。
四、总结
通过本文的介绍,相信你已经掌握了如何使用Flask搭建个人博客和在线商城。Flask的轻量级和灵活性使得它成为开发Web应用的首选框架之一。希望这篇文章能够帮助你开启自己的Web应用之旅。
