Loading

从零开始学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 %}

 

运行结果

image

posted @ 2019-11-21 09:08  江雪独钓翁  阅读(187)  评论(0编辑  收藏  举报