菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
19
0

Flask 易错点

原创
05/13 14:22
阅读数 75650

1.With上下文管理器

常用:

with open("file_name","wb") as f:
	f.write("hello flask")

自定义:

class Foo(gbiect):
	def __enter__(self):
		"""进入with语句的时候被with调用"""
		print("enter called")
	def __exit_(self, exc_type, exc_val, exc_tb):
		"""离开with语句的时候被with调用"""
        print("exit called")
        print("exc_type:%s" % exc_type)
        print("exc_val:9%s" % exc_val)
		print("exc_tb:%s"%exc_tb)
		
with Foo() as foo:
    print("helto python")
    a=1/0
    print("hello end")

运行结果:

 enter called 
  Traceback (most recent call last): 
  hello python 
	File"/Users/delron/Desktop/code/03 with. py", line 39, in <module>
  exit called
       a=1/0
  ZeroDivisionError: integer division or modulo by zero 
  exc_type:<type ' exceptions. ZeroDivisionError'>
  exc_val: integer division or modulo by zero 
  exc_tb:<traceback object at 0x1097bc440>
  Process finished with exit code 1

2. Json模块

dumps —> 可以将字典转换为字符串

    import json

    a = '{"city": "sz", "country": "china"}'

    b = json.loads(a)

    print(type(b), b)

  运行结果:

    dict {"city": "sz", "country": "china"}

3. xss攻击

  当前段传送过来的数据默认进行转义,否则,则会默认执行前端传送的数据,则称为xss攻击

4. flask 和mysql

 Linux:

  flask使用mysql数据库需要:

    1、pymysql

    2、sqlalchemy

    3、flask_sqlalchemy

  windows:

    Flask利用pymysql出现Warning:1366的解决办法

    flask使用mysql数据库需要:

mysql-connector-python

sqlalchemy

flask_sqlalchemy

SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://root:mysql@localhost/ihome01"

5. 装饰器

  @app.route("/")

  def index():

   return "index page"
  def index():
   	return "index page"
  app.route("/)(index)
装饰器不仅仅是定义时可以用,还可以在定义完再使用

6. 自定义正则转换器及蓝图

from werkzeug.routing import BaseConverter

定义正则转换器

class ReConverter(BaseConverter):

def __init__(self, url_map, regex):
    # 调用父类初始化方法
    super(ReConverter, self).__init__(url_map)
    # 重新赋值
    self.regex = regex

添加自定义的转换器

 app.url_map.converters["re"] = ReConverter
  from flask import Blueprint, current_app

  html = Blueprint("web_html", __name__)

  @html.route("/<re(r'.*'):file_name>")
  def web_html(file_name):
    if not file_name:
        file_name = "index.html"
    if file_name != "favicon.ico":
        file_name = "html/" + file_name
    return current_app.send_static_file(file_name)

注册蓝图

  • app.register_blueprint(html)

7. 登录装饰器

定义验证登录状态 的装饰器

def login_required(view_func):
    # wraps函数的作用是将wrapper内层函数的属性设置为被装饰函数view_func的属性
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        # 判断用户登录状态
        user_id = session.get("user_id")
        # 如果用户是登录状态,则执行登录状态
        if user_id is not None:
            # 将user_id保存到g对象中,在视图函数中,可以通过g对象获取保存数据
            g.user_id = user_id
            return view_func(*args, **kwargs)
        # 如果未登录,则返回未登录信息
        else:
            return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")
    return wrapper

8. 视图函数

  • 路由匹配不能出现相同的地址,即同一地址,不能出现两个视图函数

  • 路由匹配不能出现不同的函数,即不同的地址,不能出现相同的函数名

9. 参数获取

  • 直接从request中获取json数据,并将其转换为字典

    house_data = request.get_json()

  • 从request中获取文件

    image_file = request.files.get("house_image")

  • 从request中的form表单中获取键值对

    house_id = request.form.get("house_id")

10. 数据库操作

  在同一视图函数中,可以对对象多次修改,只提交一次即可

 db.session.add(house_image)
  db.session.add(house)
  try:
      db.session.commit()
  except Exception as e:
      current_app.logger.error(e)
      db.session.rollback()

发表评论

0/200
19 点赞
0 评论
收藏