我是靠谱客的博主 踏实水杯,这篇文章主要介绍Flask-01 基本组成, 项目拆分,现在分享给大家,希望可以做个参考。

    本文主要介绍如何开始一个Flask项目, 以及更具Flask的丰富的拓展包, 来拆分项目, 让一个Flask项目满足基本的MVC架构, 下面就开始进入正文.

Flask介绍:

  • Flask是一种使用Python编写的轻量级的Web框架, WSGI工具采用Werkzeug,模板引擎使用Jinja2, Flask使用的是BSD授权

  • Flask核心简单, 可以自定义扩展, 没有固定的数据库和模板等设置, 简单轻便

  • Flask也有web开发'微'框架之称

开始项目

  • 下面是官网中的一个Flask项目, 只需要简单的7行代码, 我们就是可以让一个Flask项目运行起来

  • Flask由于框架体量小, 所有的拓展包都需要我们自己去安装, 所以一个Flask项目会安装很多的拓展包是很常见的

最简单的一个Flask项目

复制代码
1
2
3
4
5
6
7
8
9
10
from flask import Flask app = Flask(__name__) @app.route('/') def hello():    return 'hello' if __name__ == '__main__':    main()

拆分

基于项目的可拓展性和代码的可阅读性, 当代码量较大时, 将所有的代码写在一个文件中是一件很可怕的事, 所以我们就要对其进行拆分, 主要还是参考 MVC模式进行, 将不同的功能放在其对应的模块中, 方便我们进行阅读

  • 将主要的执行内容写在执行文件manage.py中, 引入 Manage 进行管理app

  • 创建app的文件目录, 将views 和 models 写入其中进行管理

  • 创建 static 目录,存放静态文件

  • 创建 templates 目录, 存放网页模板

  • 创建utils 目录, 管理工具函数

Manger管理app
复制代码
1
2
3
4
5
6
7
8
9
10
from flask_script import Manager from utils.functions import create_app app = create_app() manage = Manager(app=app) if __name__ == '__main__':    manage.run()
views定义路由和控制器
  • 需要蓝图

  • blueprint

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from flask import Blueprint user_buleprint = Blueprint('user', __name__) # 'user'在重定向时使用 @user_blueprint.route('/')  # 设置路由, 访问的url def index(): return render_template('index.html')  # 返回渲染的模板页面 # 带参数返回 @user_blueprint.route('/') def index():    content = ''    return render_template('index.html', content=content)
注册蓝图
  • utils.functions.py

复制代码
1
2
3
4
5
6
7
8
9
from flask import Flask def create_app(): app = Flask(__name__)    # 注册蓝图, 设置url 前缀, 相当于 django中的 namespace    app.register_blueprint(blueprint=user_buleprint, url_prefix='/user')        return app

设置静态文件目录和模板目录
复制代码
1
2
3
4
5
6
7
8
9
10
11
def create_app():    BASE_DIR = os.path.dirname(os.path.dirname(__file__))    static_dir = os.path.join(BASE_DIR, 'static')    templates_dir = os.path.join(BASE_DIR, 'templates')    # 初始化 app    app = Flask(__name__,                static_folder=static_dir,                template_folder=templates_dir)    return app

视图函数


路由

  • @app.route('/')

客户端发送请求给服务器(浏览器到服务器), 进而请求传递到Flask的应用实例,应用实例需要知道对于各个URL请求需要执行哪部分代码, 所以它给Python函数建立了一个URLs映射, 这种在URL和函数之间建立联系的操作称之为路由

route规则

@app.route('/<int:name>')

  • string 字符串 默认

  • int 整形

  • float 浮点型

  • path 路径

  • uuid uuid字符串

  • any 任何,无限制

对同一个模板,通过url中传递的参数类型不同来执行不同的视图函数进行渲染

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from flask import render_template, request, Blueprint blue = Blueprint('first', __name__) @blue.route('/hello/') def hello():    return render_template('hello.html') @blue.route('/hello/<name>/', methods=['POST', "GET"]) def hello_name(name):    if request.method == 'GET':        return render_template('hello.html', name=name) @blue.route('/hello/<float:float>/') def float(float):    return render_template('hello.html', float=float) @blue.route('/hello/<int:int>/') def float1(int):    return render_template('hello.html', int=int) @blue.route('/hello/<path:path>/') def show(path):    return render_template('hello.html', path=path) @blue.route('/hello/<uuid:uuid>/') def uuid1(uuid):    return render_template('hello.html', uuid=uuid)

methods 请求的方法
  • 获取传递的参数

  • GET请求:request.args

  • POST请求: request.form


最后

以上就是踏实水杯最近收集整理的关于Flask-01 基本组成, 项目拆分的全部内容,更多相关Flask-01内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部