从零开始学Flask框架-第003天
Posted zhouwp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从零开始学Flask框架-第003天相关的知识,希望对你有一定的参考价值。
index.py
from flask import Flask,render_template from flask_bootstrap import Bootstrap #初始化 app = Flask(__name__) bootstrap = Bootstrap(app) #路由,处理URL 和函数之间关系的程序称为路由 @app.route(‘/‘) #视图函数,函数执行,返回结果 def index(): return render_template(‘index.html‘) @app.errorhandler(404) def page_not_found(e): return render_template(‘404.html‘), 404 @app.errorhandler(500) def internal_server_error(e): return render_template(‘500.html‘), 500 #启动服务器 if __name__ == ‘__main__‘: app.run()
base.html
{% extends "bootstrap/base.html" %} {% block title %}Flasky{% endblock %} {#包含导航条的程序基模板#} {% block navbar %} <div class="navbar navbar-inverse" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="/">TEST</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a href="/">Home</a></li> </ul> </div> </div> </div> {% endblock %} {% block content %} <div class="container"> {% block page_content %}{% endblock %} </div> {% endblock %}
index.html
{% extends "base.html" %} {% block title %}IndexTitle{% endblock %} {#{% block content %}#} {# <div class="container">#} {# {% block page_content %}<h1>Hello Flask!</h1>{% endblock %}#} {# </div>#} {#{% endblock %}#} <div class="container"> {% block page_content %}<h1>Hello Flask!</h1>{% endblock %} </div>
404.html
{% extends "base.html" %} {% block content %} <div class="container"> {% block page_content %}<h1>Not Found!!!</h1>{% endblock %} </div> {% endblock %}
运行结果
以上是关于从零开始学Flask框架-第003天的主要内容,如果未能解决你的问题,请参考以下文章