• 蓝图


    #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)
  • 相关阅读:
    牛券
    探险
    雷达安装
    智力大冲浪
    奶牛玩杂技
    BJWC2008 秦腾与教学评估
    JSOI2010 部落划分
    作诗
    ASP.NET MVC4系列验证机制、伙伴类共享源数据信息(数据注解和验证)
    正则表达式
  • 原文地址:https://www.cnblogs.com/themost/p/9271854.html
Copyright © 2020-2023  润新知