组团学

错误页面定制与视图传递多个参数

阅读 (189543)

一、错误页面定制

  • 视图函数

    @app.errorhandler(404) def page_not_found(e): return render_template('error.html',title='404NOTFOUND', info=e) @app.errorhandler(500) def page_not_found(e): return render_template('error.html',title='500ERROR', info=e)
  • 定制模板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{ title }}</title> </head> <body> <h2>{{ err }}</h2> </body> </html>

二、视图传递多个参数

  • locals

    def index(): a = 1 b = 2 return render_template('boot_index.html',**locals())
  • 将字典变成关键字 参数

    def index():
        return render_template('boot_index.html',**{'a':1,'b':2})
    
  • 原始传参

    def index(): return render_template('boot_index.html',a=1,b=2)
  • 将参数写在字典中

    def index(): return render_template('boot_index.html',val={'a':1,'b':2})
需要 登录 才可以提问哦