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.

源代碼如下:

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語句的縮進位置調整一下,

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) 

  


終於不再報錯。特此記錄,以供以后參考。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM