按照书上的步骤来的
1.在bootstrap中编写了base.html,放在templates/bootstrap目录下的
代码:
1 {% block doc -%}
2 <!DOCTYPE html>
3 <html{% block html_attribs %}{% endblock html_attribs %}>
4 {%- block html %}
5 <head>
6 {%- block head %}
7 <title>{% block title %}{{title|default}}{% endblock title %}</title>
8
9 {%- block metas %}
10 <meta name="viewport" content="width=device-width, initial-scale=1.0">
11 {%- endblock metas %}
12
13 {%- block styles %}
14 <!-- Bootstrap -->
15 <link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="stylesheet">
16 {%- endblock styles %}
17 {%- endblock head %}
18 </head>
19 <body{% block body_attribs %}{% endblock body_attribs %}>
20 {% block body -%}
21 {% block navbar %}
22 {%- endblock navbar %}
23 {% block content -%}
24 {%- endblock content %}
25
26 {% block scripts %}
27 <script src="{{bootstrap_find_resource('jquery.js', cdn='jquery')}}"></script>
28 <script src="{{bootstrap_find_resource('js/bootstrap.js', cdn='bootstrap')}}"></script>
29 {%- endblock scripts %}
30 {%- endblock body %}
31 </body>
32 {%- endblock html %}
33 </html>
34 {% endblock doc -%}
然后在templates下编写了base.html
1 {% extends "bootstrap/base.html" %}
2 {% block title %}Flasky{% endblock %}
3 {% block navbar %}
4
5
6 <p class="navbar navbar-inverse" role="navigation">
7 <p class="container">
8 <p class="navbar-header">
9 <button type="button" class="navbar-toggle"
10 data-toggle="collapse" data-target=".navbar-collapse">
11 <span class="sr-only">Toggle navigation</span>
12 <span class="icon-bar"></span>
13 <span class="icon-bar"></span>
14 <span class="icon-bar"></span>
15 </button>
16 <a class="navbar-brand" href="/">Flasky</a>
17 </p>
18 <p class="navbar-collapse collapse">
19 <ul class="nav navbar-nav">
20 <li><a href="/">Home</a></li>
21 </ul>
22 </p>
23 </p>
24 </p>
25
26
27 {% endblock %}
28
29 {% block content %}
30
31
32 <p class="container">
33 {% block page_content %}{% endblock %}
34 </p>
35
36
37 {% endblock %}
还有404.html
1 { % extends "base.html" % }
2 { % block title % }Flasky - Page Not Found{ % endblock % }
3 { % block page_content % }
4 <p class="page-header">
5 <h1>Not Found</h1>
6 </p>
7 { % endblock % }
~
接着是python脚本语言
1 from flask import Flask,render_template
2 from flask_bootstrap import Bootstrap
3
4 app = Flask(__name__)
5 Bootstrap(app)
6
7 @app.route('/user/<name>')
8 def user(name):
9 return render_template('user.html', name = name)
10 @app.errorhandler(404)
11 def page_not_found(e):
12 return render_template('404.html'),404
13
14 @app.errorhandler(500)
15 def internal_server_error(e):
16 return render_template('500.html'),500
17 if __name__ == "__main__":
18 app.run()
可是最后在网页上却显示这样的结果:
我早浏览器中输入的地址是:
127.0.0.1:5000/ab
{ % extends "base.html" % } { % block title % }Flasky - Page Not Found{ % endblock % } { % block page_content % }
Not Found
{ % endblock % }
PHP中文网2017-04-18 10:33:30
파이썬 파일에 부트스트랩이 없습니다 = 부트스트랩(앱)
부트스트랩이 없습니다 =
템플릿 엔진이 부트스트랩 모듈 디렉터리에서 템플릿을 찾을 수 없습니다
PHP中文网2017-04-18 10:33:30
파이썬 스크립트의 다섯 번째 줄: Bootstrap(app)
다음과 같이 저장할 변수를 찾아야 합니다: bootstrap = Bootstrap(app)
그렇지 않으면 참조 횟수는 0이 되고 다시 해제됩니다. 이는 초기화되지 않았음을 의미합니다.