zoukankan      html  css  js  c++  java
  • 《Flask Web开发:基于Python的Web应用开发实战》1day

    《Flask Web开发:基于Python的Web应用开发实战》

    想跟着学一遍flask,做一个博客,重点是做网页。希望,在之后的学习中,不要忘记重点以及不要放弃(还没怎么坚持过)。

    ---题记

    1. 安装

    1.1 准备

    在pycharm中新建了flask虚拟环境之后默认加载的flask程序:

    F1

    from flask import Flask

    app = Flask(__name__)


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


    if __name__ == '__main__':
       app.run()

    ---->

    image-20200224142424927

    2.程序的基本结构

    2.1初始化

    image-20200224143243570

    __name__是根目录的名字?

    2.2路由和视图函数

    路由

    image-20200224143654224

    image-20200224143801384

    image-20200224143811655

    视图函数

    image-20200224144334320

    由上图可以发现。

    route里面的路由‘/’不必非要和下面的函数名字一致,也可。

    之前,怎么写成一样,不一样还不行的?

    image-20200224144530880

    image-20200224144628548

    2.3启动服务器

    image-20200224144901605

    2.4一个完整的程序

    image-20200224144942773

    image-20200224144958867

    image-20200224145011457

    image-20200224145045216

    F2

    image-20200224145104266

    2.5请求-响应循环

    image-20200224145240375

    2.5.1程序和请求上下文

    django程序写法

    image-20200224145441445

    上下文中多个request

    image-20200224150114489

    程序和请求上下文image-20200224150757696

    image-20200224150935770

    2.5.2请求调度

    image-20200225090600916

    2.5.4请求钩子

    image-20200225094004676

    2.5.4响应

    image-20200225094900555

    image-20200225100133536

    控制权?web服务器

    F2

    from flask import redirect
    from flask import Flask
    app = Flask(__name__)
    @app.route('/')
    def index():
       return redirect('https://www.baidu.com')
    # if __name__=='__main__':
    app.run(debug=True)
    Warning1:

    Silently ignoring app.run() because the application is run from the flask command line executable. Consider putting app.run() behind an if name == "main" guard to silence this warning. app.run(debug=True)

    2.6Flask扩展

    image-20200301111606891

    社区里扩展不够,去Python标准库或代码库。

    使用Flask-Script支持命令行选项

    image-20200301113044528

    安装:

    pip install flask-script

    干啥的?

    error1

    ModuleNotFoundError: No module named 'flask.ext' 的解决方法

    出现该问题主要原因是新版的flask抛弃了flask.ext这种引入扩展的方法,更改为 flask_扩展名
    例如:
    以前:from flask.ext.script import Manager
    现在:from flask_script import Manager

    F3

    from flask_script import Manager
    from flask import Flask
    app = Flask(__name__)
    manager = Manager(app)

    if __name__ == '__main__':
       manager.run()

    解析命令行的功能?

    image-20200301122853913

    image-20200301130936180

    image-20200301131805769

    看了半圈,没明白讲的什么?

    大概是支持在shell后面扩展端口或者程序的。

    网上的资料: 通过使用Flask-Script扩展,我们可以在Flask服务器启动的时候,通过命令行的方式传入参数。而不仅仅通过app.run()方法中传参,比如我们可以通过python hello.py runserver –host ip地址,告诉服务器在哪个网络接口监听来自客户端的连接。默认情况下,服务器只监听来自服务器所在计算机发起的连接,即localhost连接。 我们可以通过python hello.py runserver --help来查看参数。

    Flask-Script插件为在Flask里编写额外的脚本提供了支持。包括了一个开发服务器,一个定制的Python命令行,用于执行初始化数据库、定时任务和其他属于web应用之外的命令行任务的脚本

    2.

    Flask-Script还可以为当前应用添加脚本命令

    """自定义flask_script终端命令"""
    from flask_script import Manager
    from flask import Flask
    app = Flask(__name__)
    from flask_script import Command
    manager = Manager(app)
    class HelloCommand(Command):
       """命令的相关描述"""
       def run(self):
           with open("text.txt", "w") as f:
               f.write("hello hello")
               pass
           print("这是执行了hello命令")

    manager.add_command('hello', HelloCommand())

    链接:https://www.jianshu.com/p/a681f6490c3c

     

  • 相关阅读:
    MySql相关
    RabbmitMQ 的配置及开启MQTT服务
    cmake 编译.so or .a文件很大问题
    模型上移动端遇到简单的问题:No variants found for 'app'
    第一次用go mod模式创建beego1.12的步骤
    python中RabbitMQ的使用hello world简单模式
    Ubuntu18.04 20.04安装rabbitMQ
    Django的mysqlclient报错
    《软件需求》读书笔记六
    《软件需求》读书笔记五
  • 原文地址:https://www.cnblogs.com/Doner/p/12359266.html
Copyright © 2011-2022 走看看