用flask写一个博客蓝图
时间: 2023-11-21 10:57:46 浏览: 35
好的,以下是一个使用 Flask 实现博客蓝图的示例:
```python
from flask import Blueprint, render_template, request, redirect, url_for
blog = Blueprint("blog", __name__, template_folder="templates")
@blog.route("/")
def index():
return render_template("index.html")
@blog.route("/post/<int:post_id>")
def post(post_id):
return render_template("post.html", post_id=post_id)
@blog.route("/create", methods=["GET", "POST"])
def create():
if request.method == "POST":
# 保存新博客帖子
return redirect(url_for(".index"))
return render_template("create.html")
```
在主 Flask 应用中,可以这样注册博客蓝图:
```python
from flask import Flask
from .blog import blog
app = Flask(__name__)
app.register_blueprint(blog)
```
请注意,以上代码仅提供了一个大致的框架,并不是完整的博客应用。你需要为路由添加具体的逻辑,并实现如何存储博客帖子。