淘先锋技术网

首页 1 2 3 4 5 6 7

可以在我的github获得更好的markdown阅读体验。

Flask-Bootstrap

pip install flask-bootstrap
  • 修改hello.py代码
    from flask import Flask,render_template
    # import bootstrap
    from flask.ext.bootstrap import Bootstrap

    app = Flask(__name__)
    # Bootstrap app
    bootstrap = Bootstrap(app)

    @app.route('/')
    def index():
        # use index.html modle
        return render_template('index.html')

    if __name__ == '__main__':
        app.run(host='0.0.0.0',port=)
  • 创建templates文件夹

Flask中的模板放在templates文件夹中。
* 创建index.html 文件

    {% extends "base.html" %}

    {% block title %}首页{% endblock %}

    {% block page_content %}
    <h1>这里是首页,Hello World!</h1>
    {% endblock %}

  • 创建base.html文件
    {% extends "bootstrap/base.html"  %}

    {% block title %}Flask{% 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="/">Flasky</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 %}
  • ok,现在输入python hello.py启动程序

应该看到如下页面:

说明我们成功调用的bootstrap的index页面,YES!