蓝图

#coding=utf-8

from flask import Blueprint, request, render_template, redirect, url_for, flash
#创建一个蓝图 传入参数分别是蓝图名称,蓝图所在目录,必选, 其余为可选,如果静态文件所在目录
book_bp = Blueprint("book", __name__, template_folder="../templates")
books = ["The lord of the ring", "Head first html and css", "The Next"]

@book_bp.route("/", methods=["GET"])
def index():
    return "Hello, Welcome to my book library"


@book_bp.route("/book", methods=["GET", "POST"])
def handle_book():
    if request.method == "POST":
        title = request.form.get("title")
        if title:
            books.append(title)
            flash("Add book succussfully.")
            return redirect(url_for("book.handle_book"))
    return render_template("book.html", books=books)

@book_bp.route("/book/<name>")
def get_book_info(name):
    book = [name]
    if not name in books:
        book = []
    return render_template("book.html", books=book)


#coding=utf-8
#注册蓝图
from flask import Flask
from flask import render_template
from book.book import book_bp


app = Flask(__name__)
app.secret_key = "YouWillNeverGuessWhatIThink"

app.register_blueprint(book_bp)

@app.route("/home")
def hello_world():
    return "hello world"


@app.errorhandler(404)
def page_not_found(error):
    return render_template("404.html")




if __name__ == '__main__':
    app.run(debug=True)

 

转载于:https://www.cnblogs.com/themost/p/9271854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值