Flask framework to learn from scratch - the first 003 days

index.py

from Flask Import the Flask, the render_template
 from flask_bootstrap Import on Bootstrap
 # initialization 
App = the Flask ( the __name__ ) 
on Bootstrap = on Bootstrap (App) 

# routing, the relationship between the URL and the program processing functions called routing 
@ app.route ( ' / ' )
 # view function, the function execution result is returned 
DEF index ():
     return the render_template ( ' index.html ' ) 

@ app.errorhandler ( 404 )
 DEF page_not_found (E):
     return the render_template ( '404.html ' ), 404 
@ app.errorhandler ( 500 )
 DEF INTERNAL_SERVER_ERROR (E):
     return the render_template ( ' 500.html ' ), 500 # boot server IF the __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 %}


operation result

image

Guess you like

Origin www.cnblogs.com/zhouwp/p/11903549.html
Recommended