蓝图

#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)

 

posted @ 2018-07-06 09:23  道高一尺  阅读(241)  评论(0编辑  收藏  举报