zoukankan      html  css  js  c++  java
  • Flask-基本原理与核心知识

    虚拟环境

    1. 使用pipenv创建一个虚拟环境和项目绑定,安装:E:pyqiyueflask>python3 -m pip install pipenv
    2. 和项目绑定:到项目的目录中pipenv install ,然后pipenv shell,就进入到了虚拟环境中了,然后就可以安装各种包了,比如pipenv install flask,
    3. 常见的pipenv命令,退出exit,进入pipenv shell,安装pipenv install 包名,卸载pipenv uninstall 包名,查看安装包的依赖关系pipenv graph
    4. 与pycharm绑定,在虚拟环境中pipenv --venv,就会显示一个对应的目录,然后在pycharm中选择环境时绑定一下就可以了,这样运行的就是这个虚拟环境了

    flask最小原型与唯一URL原则

    1. flaks的最小原型,此时访问http://127.0.0.1:5000/hello就可以看到return的值了
    # -*- coding: utf-8 -*-
    from flask import Flask
    # 实例化
    app = Flask(__name__)
    
    @app.route('/hello')
    def hello():
        return 'hello'
    app.run()
    
    1. 唯一URL原则,此时若用户访问http://127.0.0.1:5000/hello/网址,因为多了一个斜杠,那么这个页面就找不到,若此时@app.route('/hello/')这里多个一个斜杠,就可以访问了,
    2. 但是此时访问不带斜杠的也是可以的,那时因为flask底层做了一次重定向

    路由的另一种注册方法

    1. 因为每一次修改完代码还的重启服务器才能看见效果,所以这个时候开启debug模式就可以了,在app.run(debug=True)添加就行了,这样就方便多了
    2. 路由的另一种注册方法,使用app调用add_url_rule()方法就可以了
    # -*- coding: utf-8 -*-
    from flask import Flask
    # 实例化
    app = Flask(__name__)
    
    def hello():
        return 'hello'
    
    # 路径,视图函数
    app.add_url_rule('/hello',view_func=hello)
    
    app.run(debug=True)
    
    1. 但是通常的情况下还是使用装饰器就可以了

    app.run相关参数与flask配置文件

    1. 此时的app.run(debug=True)这个情况下,只有通过127.0.0.1:5000才能访问,即使输入本机ip也不能访问,所以这个时候可以在这个方法中添加参数,此时在地址栏中输入http://192.168.2.14:81/hello也是可以访问的
    # host:指定ip地址,输入本机ip也可以访问了,port是改变默认的端口
    app.run(host='0.0.0.0',debug=True,port=81)
    
    1. 若项目上线的话,此时debug模式就不能开启,因为要保持源代码的一致性,所以此时尽量不能删除这个debug,这个时候写一个配置文件,在目录中新建一个config.py
    # -*- coding: utf-8 -*-
    DEBUG = True
    
    
    1. 这个时候在文件中, 先载入这个文件,然后进行读取,注意参数要大写
    # -*- coding: utf-8 -*-
    from flask import Flask
    
    app = Flask(__name__)
    # 载入这个配置文件,这里要注意路径,
    app.config.from_object('config')
    
    @app.route('/hello')
    def hello():
        return 'hello'
    # 读取配置文件中信息
    app.run(host='0.0.0.0',debug=app.config['DEBUG'],port=81)
    

    if name 的作用

    1. 有这个判断的话,只有这个文件作为入口文件的情况下,这个run才会执行
    if __name__ == '__main__':
        app.run(host='0.0.0.0',debug=app.config['DEBUG'],port=81)
    
    1. 生成环境下通常不会使用flask自带的服务器,而是使用nginx+uwsgi,此时这个文件就不是入口文件了,这个时候uwsgi也是一个服务器,若没有加判断就会同时有两个服务器了,所以这个加上判断是必要的

    Response

    1. 视图函数返回的其实就是一个Response,若这样写,此时页面上什么也没有,默认的content-type = text/html
    @app.route('/hello')
    def hello():
        return '<html></html>'
    
    
    1. 返回的有这么几个重要的参数status code,content-type,也可以自定义,此时返回的<html></html>,因为使用的文本解析,当然了还可以返回json application/json
    from flask import Flask,make_response
    @app.route('/hello')
    def hello():
        # status code 200,404,301,状态码只是一个标识
        # content-type http headers,指定了客户端在接收到数据后用什么方式进行解析
        # content-type = text/html 默认的
        # Response对象
        headers = {
            'content-type':'text/plain',#文本解析
        }
        response = make_response('<html></html>',404)
        response.headers = headers
        return response
    
    
    1. 此时还可以进行重定向,只需要把状态码改成301,在headers里添加一个location就可以了,而且还可以写的简洁写,这样就可以了
    @app.route('/hello')
    def hello():
            headers = {
            'content-type':'text/plain',
            'location':'http://www.bing.com'
        }
        return '<html></html>',301,headers    
    
  • 相关阅读:
    10. Regular Expression Matching
    9. Palindrome Number
    6. ZigZag Conversion
    5. Longest Palindromic Substring
    4. Median of Two Sorted Arrays
    3. Longest Substring Without Repeating Characters
    2. Add Two Numbers
    链式表的按序号查找
    可持久化线段树——区间更新hdu4348
    主席树——树链上第k大spoj COT
  • 原文地址:https://www.cnblogs.com/mengd/p/9477888.html
Copyright © 2011-2022 走看看