flask学习,关于4.2.2 输出HTML代码报错的问题

问题描述

今天在学习第四章表单时,创建form实例时访问实例属性时报错,即以下代码出错

	form = LoginForm()
    print(form.username)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

在论坛里看到同样的问题,问题很明确,需要请求上下文:
问题链接

解决方案

在测试的时候加上请求上下文就可以了

    with app.test_request_context():
        form_new = LoginForm()
        print(form_new.username)

然后就可以正常打印转化后的HTML代码。

保持对优秀的热情
原文地址:https://www.cnblogs.com/luckforefforts/p/13642708.html