zoukankan      html  css  js  c++  java
  • flask的使用(一)

    1.程序基本的说明

    #-*-encoding=utf-8-*-
    从flask中引入类
    from flask import Flask ,render_template
    import config
    初始化一个flask对象 Flask(),需要传入一个参数_name_
    1.方便flask寻找寻找资源
    2.方便flask插件,比如Flask Sqlalcheny出现错误的时候,好去寻找问题所在的位置
    app = Flask(__name__)
    @开头,并且是在函数上面,说明是一个装饰器
    这个装饰器的作用,是做一个url与视图函数的映射相匹配的
    比如127.0.0.1:5000/————>去请求一个heloo_world这个项目,额庵后返回给浏览器
    @app.route('/')
    def hello_world():
        return render_template('index.html',username='kanhwang',gender='nan')
    如果当前这个文件是作为程序的一个入口程序,那么就执行——app.run()这个方法
    if __name__ == '__main__':
        app.run()
    app.run ()启动一个应用服务器,来接受用户的一个请求
    相当于while True:
    Listen()
    #static:用来存储静态资源的
    #tem中放入的数html文件

    2.debug模式的开启

      好处:可以修改代码,而不需要重新的加载程序,他会在自己执行ctrl+s之后,自动将程序加载。但是HTML页面是不可以的,在java中也是这样的,但是在java中需要保存,可以不用启动服务器。

      开启方式:

       (1)在主函数的run方法中添加debug==True

       (2)可以使用配置的方法 

    Import config
    App.config.form_obiect(config)

    config中的程序

    DEBUG=True

    3.url传递参数

     3.1.参数的作用,可以在相同的Url中,指定不同的参数,来加载不同的数据

        3.2。在flask中使用参数,

            要求:参数必须放在两个括号中,视图中的需要放在url中的参数同名

    @app.route('/kw/<id>')              两个括号之间
    def kw(id):
        return u"你传入的参数是%s" % id    视图中与url中的同名

    4.页面渲染

      4.1flask建项目的目录说明  static中存放的是css、js等页面的,在template中放置的是页面HTML。

      4.2页面渲染的使用案例

    from flask import Flask ,render_template
    import config
    app = Flask(__name__)
    @app.route('/')
    def hello_world():
        return render_template('index.html',username='kangwang',gender='man')
    if __name__ == '__main__':
        app.run()

    页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Demo1</title>
    </head>
    <body>
    用户名:{{ username}}
    性别:{{ gender }}
    </body>
    </html>

    4.3数据传入

       上面的例子中传输参数,在template方法里面,但是如果有太多的页面,这样的传输,不是一个好的办法,那么我们可以使用字典的方式传输值

    @app.route('/')
    def hello_world():
        context={
            'username':'kanhwang',
            'gender':'nan'
        }
        return render_template('index.html',**context)

    传入类的方式

    def hello_world():
        class people:
            name='kw'
            age=23
    
        p=people()
        context = {
            'username': 'kanhwang',
            'gender': 'nan',
            'peo':p
        }
        return render_template('index.html',**context)

    接收端

    用户名:{{ peo.name}}
    性别:{{ peo.age }}

    传输字典的方式

    context = {
        'username': 'kangwang',
        'gender': 'man',
        'peo':p,
        'websites':{
            'baidu':'http://baidu.com',
            'hh':'https://i.cnblogs.com/EditPosts.aspx?opt=1'
        }
    }

    接收数据端

    {{websites.baidu }}

    还有一种方式,使用与字典,也适用于类传递参数

    {{websites['baidu'] }}
  • 相关阅读:
    15 Django组件-中间件
    Android学习笔记-Dialog详解
    python爬取世界疫情信息到Mysql
    《构建之法》阅读笔记3
    团队项目--校园百晓生
    团队项目--校园百晓生
    php安装配置及问题解决
    《构建之法》阅读笔记2
    云服务器配置(转载)
    第五周总结
  • 原文地址:https://www.cnblogs.com/kw28188151/p/8179818.html
Copyright © 2011-2022 走看看