前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >慕课网Flask高级编程实战-1.项目准备 和 Flask入门

慕课网Flask高级编程实战-1.项目准备 和 Flask入门

作者头像
Meet相识
发布2018-09-12 16:50:00
1.2K3
发布2018-09-12 16:50:00
举报
文章被收录于专栏:技术专栏

1.pipenv 按照与使用

gitub地址
代码语言:javascript
复制
# 按照pipenv
pip install pipenv

# 进入虚拟环境(需要在项目目录上执行,如果当前目录没有,则会新建)
pipenv shell

# 退出虚拟环境
exit

# 使用pipenv按照类库
pipenv install #{package}

# 卸载类库
pipenv uninstall #{package}

# 查看按照包的依赖关系
pipenv graph

# 查看虚拟环境执行文件路径
pipenv --venv

2.Flask的实例化和启动

代码语言:javascript
复制
# 实例化,构造函数的参数会作为Flask app核心对象的标识
app = Flask(__name__)
# 启动web app
# debug=True开启调试模式 1.修改文件后不用每次都重启服务器 2.可以在网页中显示异常
# host可以指定可以访问本网站的ip地址
# port启动端口
app.run(host="0.0.0.0", port=81, debug=True)

3.注册路由-法1

代码语言:javascript
复制
# 通过@app.route装饰器来注册路由,其中的参数为url路径。对应会调用hello()函数
@app.route("/hello")
def hello():
    """定义视图函数-mvc 中的controller"""
    return "我就不说hello world"

如果直接使用/hello 那么不能兼容浏览器不能访问/hello/和/hello,解决办法,路径定义改成/hello/

代码语言:javascript
复制
@app.route("/hello/")
def hello():
    return "我就不说hello world"

在路径xxx后添加斜杠/后就可以访问xxx/ 和 xxx的原理

重定向:当你访问url1的时候,服务器返回状态码302让用户访问url2

重定向

访问http://localhost:5000/hello,观察浏览器网络请求情况,可以看到发生了重定向,重定向到了hello/

image.png

为什么这么做呢?

这是因为,如果不做重定向不带/ 的(hello)和带/的(hello/)都可以访问到视图函数,那么就是说同一个视图函数对应着两个不同的路由,没有保证唯一url的原则

唯一url的好处

如果有两个url,那么在搜索引擎中会被索引两次,这样会浪费性能,影响搜索引擎的优化,没有这个必要。

4.注册路由-法2

另一种注册路由的函数,通过调用app的add_url_rule函数

代码语言:javascript
复制
app.add_url_rule("/hello",view_func=hello)

实际上方法1装饰器的模式,内部就是调用了add_url_rule函数。下面来看一下源码

代码语言:javascript
复制
    def route(self, rule, **options):
        """A decorator that is used to register a view function for a
        given URL rule.  This does the same thing as :meth:`add_url_rule`
        but is intended for decorator usage::

            @app.route('/')
            def index():
                return 'Hello World'

        For more information refer to :ref:`url-route-registrations`.

        :param rule: the URL rule as string
        :param endpoint: the endpoint for the registered URL rule.  Flask
                         itself assumes the name of the view function as
                         endpoint
        :param options: the options to be forwarded to the underlying
                        :class:`~werkzeug.routing.Rule` object.  A change
                        to Werkzeug is handling of method options.  methods
                        is a list of methods this rule should be limited
                        to (``GET``, ``POST`` etc.).  By default a rule
                        just listens for ``GET`` (and implicitly ``HEAD``).
                        Starting with Flask 0.6, ``OPTIONS`` is implicitly
                        added and handled by the standard request handling.
        """
        def decorator(f):
            endpoint = options.pop('endpoint', None)
            # 可以看到这里就是调用了add_url_rule,self就是我们实例化的app对象
            self.add_url_rule(rule, endpoint, f, **options)
            return f
        return decorator

5.falsk配置文件

代码语言:javascript
复制
# 载入配置文件
app.config.from_object("config")
# 读取配置文件
print(app.config["DEBUG"])

陷阱1:配置文件中的key必须是全大写 陷阱2:DEBUG是flask里的一个默认变量,如果想要覆盖,必须名字一模一样

5.if name = "main"

简单理解:加入这个判断以后,只有在启动入口文件以后,才会执行。而在被其他模块导入的时候不会执行

深入理解其原因是:在生产环境中,我们是使用nginx+uwsgi来部署python项目,其中uwsgi用来启动flask服务,这个时候,根本fisher.py只是作为一个模块被调用,这时加上入口判断,在生产环境中就只不会执行app.run方法。否则的话就会启动两个flask服务。

代码语言:javascript
复制
if __name__ == "__name__":
    app.run(host=app.config["HOST"], debug=app.config["DEBUG"], port=app.config["PORT"])
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.05.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.pipenv 按照与使用
    • 2.Flask的实例化和启动
      • 3.注册路由-法1
        • 在路径xxx后添加斜杠/后就可以访问xxx/ 和 xxx的原理
      • 4.注册路由-法2
        • 5.falsk配置文件
          • 5.if name = "main"
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档