Python TypeError:view函数未返回有效响应。函数返回None或结束时没有返回语句

Python TypeError:view函数未返回有效响应。函数返回None或结束时没有返回语句,python,python-3.x,flask,chatbot,Python,Python 3.x,Flask,Chatbot,请让我知道我不包括退货声明的地方。或者问题出在其他地方。我正在使用Python 3.6和Flask。我想做一个机器人。这个机器人在终端上工作得很好。现在我正在尝试给它一个UI并使用Flask def chat(user_input): print("Start talking with the bot !! Press q to quit ") print("You :"+user_input) while True: #inp=input("You: "

请让我知道我不包括退货声明的地方。或者问题出在其他地方。我正在使用Python 3.6和Flask。我想做一个机器人。这个机器人在终端上工作得很好。现在我正在尝试给它一个UI并使用Flask

def chat(user_input):
    print("Start talking with the bot !! Press q to quit ")
    print("You :"+user_input)
    while True:
        #inp=input("You: ")
        if user_input.lower() == "q":
            break

        results = model.predict([bag_of_words(user_input, words)])
        arr_result = results[0]
        print(arr_result)

        # gives index of the greatest number
        results_index = np.argmax(results)
        print('The result_index is '+str(results_index))

        # gives the relevant tag
        tag = labels[results_index]
        print('The tag is ' + tag)
        print('labels are'+str(labels))

        if arr_result[results_index] > 0.6:
            print('arr_result[results_index]>0.6 ' +
                  str(arr_result[results_index]))
            # get a random response from the json file
            for tg in data["intents"]:
                print(tg)
                if tg['tag'] == tag:
                    response = tg['responses']
                    print('the array of response is' + str(response))
                    rand_response = random.choice(response)

                    bot_response = str(rand_response)
                    print("Bot: "+bot_response)
                    return render_template('index.html', user_input=user_input, bot_response=bot_response)
                else:
                    print("Tag not found "+bot_response)
                    return render_template('index.html', user_input=user_input, bot_response="Sorry, I do not understand")

        else:
            bot_response = "Sorry, I do not understand"
            print("Bot: "+bot_response)
            return render_template('index.html', user_input=user_input, bot_response=bot_response)
我不断地发现以下错误--


while
循环的末尾有一个隐式
返回None

对于Flask请求处理程序,您可能不希望该函数中有循环

您可以通过只使用一条
return
语句来简化操作-毕竟,在各种返回中唯一改变的是
bot\u response

def聊天(用户输入):
结果=模型。预测([一袋一袋的单词(用户输入,单词)])
arr_result=结果[0]
打印(arr_结果)
结果\u索引=np.argmax(结果)
打印(“结果索引为”+str(结果索引))
标签=标签[结果\索引]
打印(“标签为”+标签)
打印(“标签为”+str(标签))
bot\u response=“对不起,我不明白”
如果arr_结果[结果索引]>0.6:
打印(“arr_结果[结果索引]>0.6”+str(arr_结果[结果索引]))
#从json文件获取随机响应
对于数据中的tg[“意图”]:
打印(tg)
如果tg[“标记”]==标记:
响应=tg[“响应”]
打印(“响应数组为”+str(响应))
随机响应=随机选择(响应)
机器人响应=str(随机响应)
打印(“Bot:+Bot\u响应)
其他:
打印(“未找到标签”+机器人响应)
返回呈现模板(“index.html”,用户输入=用户输入,机器人响应=机器人响应)

一个问题是,当用户点击“q”以中断循环时,没有返回。

是。你完全正确。谢谢你。让我很快纠正它。嘿,我在“q”休息下回来了。我仍然得到相同的错误。@KDV仔细查看错误后发现退出的原因是异常。乍一看,我找不到它的原因,但使用try/except捕捉异常并显示它应该会告诉您出了什么问题。我使用try/except,它只会给我
TypeError:view函数没有返回有效的响应。函数返回None或结束时没有返回语句。
。。。你把try/except放在聊天功能里面了吗?如果是,则表明某个渲染模板行为不正常。你能确认index.html存在并且正常工作吗?如果是,则可能是传递给它的某个空值。是的,我将它放在chat()中。index.html的工作方式与我从中获取用户输入的方式相同
127.0.0.1 - - [08/Mar/2020 02:40:27] "[35m[1mPOST /process HTTP/1.1[0m" 500 -
Traceback (most recent call last):
  File "C:\Users\Kuldeep\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\Kuldeep\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\Kuldeep\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Kuldeep\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\Kuldeep\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\Kuldeep\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    return self.finalize_request(rv)
  File "C:\Users\Kuldeep\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1967, in finalize_request
    response = self.make_response(rv)
  File "C:\Users\Kuldeep\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2097, in make_response
    "The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
127.0.0.1 - - [08/Mar/2020 02:40:27] "[37mGET /process?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Mar/2020 02:40:27] "[37mGET /process?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Mar/2020 02:40:27] "[37mGET /process?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Mar/2020 02:40:27] "[37mGET /process?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Mar/2020 02:40:27] "[37mGET /process?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1[0m" 200 -