Flask消息闪现及日志记录

消息闪现

Flask 提供了一个非常简单的方法来使用闪现系统向用户反馈信息。闪现系统使得在一个请求结束的时候记录一个信息,然后在且仅仅在下一个请求中访问这个数据。Flask消息闪现可以给用户更好的体验。比如,我们改造一下上一篇中的上传图片代码,加入消息闪现功能。

首先,在方法前加上app.secret_key = 'some_secret',如果缺少,报错:
RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.

对上文的上传照片代码略微改动,如下:

@app.route('/up')
def up():
    return render_template('up.html')

# 上传文件
@app.route('/up_photo', methods=['POST'], strict_slashes=False)
def api_upload():
    file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER'])
    if not os.path.exists(file_dir):
        os.makedirs(file_dir)
    f = request.files['photo']
    if f and allowed_file(f.filename):
        fname = secure_filename(f.filename)
        print fname
        ext = fname.rsplit('.', 1)[1]
        new_filename = Pic_str().create_uuid() + '.' + ext
        f.save(os.path.join(file_dir, new_filename))
        flash("photo upload success")
        return redirect(url_for('up'))
        #return jsonify({"success": 0, "msg": "上传成功"})
    else:
        return jsonify({"error": 1001, "msg": "上传失败"})


模板展示代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传</title>
</head>
<body>
    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <ul class=flashes>
        {% for message in messages %}
          <li>{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}
    <form id="form1" method="post" action="/up_photo" enctype="multipart/form-data">
        <div>
            <input id="File1" type="file" name="photo"/>
            <input type="submit">提交</input>
        </div>
    </form>
</body>
</html>

选择图片,点击上传之后,显示如图所示


而当我们刷新页面之后,闪现消息消失。

分类闪现,当闪现一个消息时,是可以提供一个分类的。这更符合我们实际开发中的场景,例如,当文件上传成功时,我们提示消息“photo upload success”为绿色;上传文件失败时,我们提示消息“photo upload error”为红色。

@app.route('/up_photo', methods=['POST'], strict_slashes=False)
def api_upload():
    file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER'])
    if not os.path.exists(file_dir):
        os.makedirs(file_dir)
    f = request.files['photo']
    if f and allowed_file(f.filename):
        fname = secure_filename(f.filename)
        print fname
        ext = fname.rsplit('.', 1)[1]
        new_filename = Pic_str().create_uuid() + '.' + ext
        f.save(os.path.join(file_dir, new_filename))
        flash("photo upload success", "success")
        return redirect(url_for('up'))
        #return jsonify({"success": 0, "msg": "上传成功"})
    else:
        flash("photo upload error", "error")
        return redirect(url_for('up'))
        #return jsonify({"error": 1001, "msg": "上传失败"})


模板展示代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传</title>
</head>
<body>
    {% with messages = get_flashed_messages(category_filter=['success'])%}
      {% if messages %}
        <ul>
        {% for message in messages %}
          <li style="color:green">{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}
    
    {% with messages = get_flashed_messages(category_filter=['error'])%}
        {% if messages %}
        <ul>
        {% for message in messages %}
          <li style="color:red">{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}
    <form id="form1" method="post" action="/up_photo" enctype="multipart/form-data">
        <div>
            <input id="File1" type="file" name="photo"/>
            <input type="submit">提交</input>
        </div>
    </form>
</body>
</html>

这样,上传成功或者失败之后,页面提示更加符合习惯





log记录

日志记录的级别(来源:https://blog.csdn.net/iszhenyu/article/details/56846551):

ERROR:这个级别的日志意味着系统中发生了非常严重的问题,必须有人马上处理,比如数据库不可用了,系统的关键业务流程走不下去了等等。很多人在实际开发的时候,不会去区分问题的重要程度,只要有问题就error记录下来,其实这样是非常不负责任的,因为对于成熟的系统,都会有一套完整的报错机制,那这个错误信息什么时候需要发出来,很多都是依据单位时间内error日志的数量来确定的。因此如果我们不分轻重缓急,一律error对待,就会徒增报错的频率,久而久之,我们的救火队员对错误警报就不会那么在意,这个警报也就失去了原始的意义。

WARN:发生这个级别的问题时,处理过程可以继续,但必须要对这个问题给予额外的关注。假设我们现在有一个系统,希望用户每一个月更换一次密码,而到期后,如果用户没有更新密码我们还要让用户可以继续登录,这种情况下,我们在记录日志时就需要使用WARN级别了,也就是允许这种情况存在,但必须及时做跟踪检查。

INFO:这个级别的日志我们用的也是比较多,它一般的使用场景是重要的业务处理已经结束,我们通过这些INFO级别的日志信息,可以很快的了解应用正在做什么。我们以在12306上买火车票为例,对每一张票对应一个INFO信息描述“[who] booked ticket from [where] to [where]”。

DEBUG和TRACE:我们把这两个级别放在一起说,是应为这两个级别的日志是只限于开发人员使用的,用来在开发过程中进行调试,但是其实我们有时候很难将DEBUG和TRACE区分开来,一般情况下,我们使用DEBUG足以。

调用日志记录的示例:

app.logger.debug('A value for debugging')
app.logger.warning('A warning occurred (%d apples)', 42)
app.logger.error('An error occurred')


如,放在我们上面的代码中,我们希望在上传成功之后,把这条信息记录一下:

app.logger.info(fname+'上传成功了.....')



这里包括闪现消息那部分,如果提示消息是中文提示编码问题,此时需要在开始地方加上:

import sys
...
reload(sys)
sys.setdefaultencoding('utf-8')

猜你喜欢

转载自blog.csdn.net/weixin_36380516/article/details/80394826
今日推荐