https://github.com/famousdraw

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)

  


终于不再报错。特此记录,以供以后参考。

posted on   红色MINI  阅读(3218)  评论(0编辑  收藏  举报

编辑推荐:
· 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 让容器管理更轻松!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示