Flask-01 基本组成, 项目拆分

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

Flask介绍:

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

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

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

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

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

最简单的一个Flask项目

  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
  
  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

  
  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

  
  from flask import Flask
  
  def create_app():
      app = Flask(__name__)
      # 注册蓝图, 设置url 前缀, 相当于 django中的 namespace
      app.register_blueprint(blueprint=user_buleprint, url_prefix='/user')
      
      return app

设置静态文件目录和模板目录
  
  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中传递的参数类型不同来执行不同的视图函数进行渲染

  
  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


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值