zoukankan      html  css  js  c++  java
  • 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
Copyright © 2011-2022 走看看