美文网首页
Flask 实践1

Flask 实践1

作者: 可爱喵星人 | 来源:发表于2018-03-09 09:19 被阅读0次

    1.一个简单的应用

    #app.py
    from flask import Flask
    
    app = Flask('__name__')
    
    @app.route('/')
    def home():
        return 'hello world'
    
    if __name__ == '__main__':
        app.run()
    

    命令行执行 python app.py
    浏览器输入:http://localhost:5000/

    simpleapp.png

    2.跳转到静态html

    #app.py
    from flask import Flask
    
    app = Flask('__name__', static_url_path='')
    
    
    @app.route('/')
    def home():    
        return app.send_static_file('view/index.html')
    
    
    @app.route('/sigin', methods=['GET'])
    def singin():
        return app.send_static_file('view/singin.html')
    
    
    @app.route('/sigin', methods=['POST'])
    def main():
        return app.send_static_file('view/main.html')
    
    
    if __name__ == '__main__':
        app.run()
    
    • static_url_path: 指定静态文件路径,默认文件夹名static
    • app.send_static_file:发送静态文件。

    文件结构:


    图片.png
    • index.html
    <h1>Home</h1>
    
    • singin.html
    <html>
        <form action="/sigin" method="post">
            <p><input name="username"></p>
            <p><input name="password"></p>
            <p><button type="submit">Sign In</button></p>
        </form>
    </html>
    
    • main.html
    <h3>hello world</h3>
    

    3.模板

    • 做一个模板
      默认情况下,模板会从templates路径下加载。


      图片.png

    模板内容大致如下:

    <html>
    <head>
      <title>Error Page</title>
    </head>
    <body>
      <p>Username ( {{ username }} )is error or password ({{pwd}}) is error</p>
    </body>
    </html>
    
    • 使用这个模板
      使用flask里面的render_template方法返回这个模板。具体代码是把
      之前那个app.py内容改成下面这样:
    from flask import Flask, request, render_template
    
    app = Flask('__name__', static_url_path='')
    
    
    @app.route('/')
    def home():
       return app.send_static_file('view/index.html')
    
    
    @app.route('/sigin', methods=['GET'])
    def singin():
       return app.send_static_file('view/singin.html')
    
    
    @app.route('/sigin', methods=['POST'])
    def main():
       name = request.form['username']
       password = request.form['password']
       if (name == 'user' and password == 'pwd'):
           return app.send_static_file('view/main.html')
       else:
           return render_template('error.html', username=name, pwd=password)
    
    
    if __name__ == '__main__':
       app.run()
    
    • render_template 函数
      这个函数主要是逻辑是:
      传入数据和模板 组装 ⇒组装后数据send给前台
    def render_template(template_name_or_list, **context):
        """Renders a template from the template folder with the given
        context.
    
        :param template_name_or_list: the name of the template to be
                                      rendered, or an iterable with template names
                                      the first one existing will be rendered
        :param context: the variables that should be available in the
                        context of the template.
        """
        ctx = _app_ctx_stack.top
        ctx.app.update_template_context(context)
        return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
                       context, ctx.app)
    
    def _render(template, context, app):
        """Renders the template and fires the signal"""
        before_render_template.send(app, template=template, context=context)
        rv = template.render(context)
        template_rendered.send(app, template=template, context=context)
        return rv
    

    参考

    flask官网: http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application
    廖雪峰Python:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432012745805707cb9f00a484d968c72dbb7cfc90b91000

    相关文章

      网友评论

          本文标题:Flask 实践1

          本文链接:https://www.haomeiwen.com/subject/mvqyfftx.html