FLASK报错,TypeError,需要valid response
今日继续调试abstract的flask框架项目,遇到这样的报错。
TypeError
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
源代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from app import app from flask import render_template,request import sys sys.path.append( 'examples' ) sys.path.append( 'src' ) import testrun @app .route( '/' ) @app .route( '/index' ) #def index(): # return render_template('index.html', title='请您输入') @app .route( '/index' ,methods = [ 'GET' , 'POST' ]) def index(): input_text = '' output_text = '' if request.method = = 'POST' : print ( 'post' ) # 接收数据 input_text = request.form.get( 'input_text' ) with open ( 'data/test_news.txt' , 'w' , encoding = 'utf=8' ) as f: f.writelines(input_text) output_text = testrun.test( ) print (output_text) return render_template( 'index.html' ,title = '文本摘要结果' , input_text = input_text,abstract_text = output_text) |
后来,看到另外一篇文章
Flask 使用过程
被route()装饰器所装饰的functions()必须有返回值
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
'Hello World!' # 没有关键字return
if __name__ == '__main__':
app.run()
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
受到启示,函数必须有返回值!
把return语句的缩进位置调整一下,
1 2 3 4 5 6 7 8 9 10 11 12 | def index(): input_text = '' output_text = '' if request.method = = 'POST' : print ( 'post' ) # 接收数据 input_text = request.form.get( 'input_text' ) with open ( 'data/test_news.txt' , 'w' , encoding = 'utf=8' ) as f: f.writelines(input_text) output_text = testrun.test( ) print (output_text) return render_template( 'index.html' ,title = '文本摘要结果' , input_text = input_text,abstract_text = output_text) |
终于不再报错。特此记录,以供以后参考。
自然语言处理爱好者,欢迎交流。QQ: 7214218
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!