【问题标题】:TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement [duplicate]TypeError:视图函数没有返回有效响应。该函数返回 None 或在没有返回语句的情况下结束 [重复]
【发布时间】:2020-06-20 17:40:41
【问题描述】:

请让我知道我不包括退货声明的地方。或者是其他地方的问题。我正在使用 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)

我不断收到以下错误--

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 -

【问题讨论】:

    标签: python python-3.x flask chatbot


    【解决方案1】:

    while 循环的末尾有一个隐含的return None

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

    您可以通过只有一个 return 语句来简化事情 - 毕竟在各种返回中唯一改变的是 bot_response

    def chat(user_input):
        results = model.predict([bag_of_words(user_input, words)])
        arr_result = results[0]
        print(arr_result)
        results_index = np.argmax(results)
        print("The result_index is " + str(results_index))
        tag = labels[results_index]
        print("The tag is " + tag)
        print("labels are " + str(labels))
        bot_response = "Sorry, I do not understand"
        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)
                else:
                    print("Tag not found " + bot_response)
        return render_template("index.html", user_input=user_input, bot_response=bot_response)
    

    【讨论】:

    • 是的。你是绝对正确的。感谢你。让我尽快纠正它。
    【解决方案2】:

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

    【讨论】:

    • 嘿,我在'q'休息时返回了。我仍然遇到同样的错误。
    • @KDV 仔细查看错误表明退出的原因是异常。乍一看我找不到原因,但使用 try/except 捕获异常并显示它应该告诉你出了什么问题。
    • 我使用了 try/except,它只是给了我TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
    • 奇怪...你把 try/except 放在那个聊天功能里面了吗?如果是,这表明您的 render_template 行为不端之一。您能否确认 index.html 存在并且正常工作?如果是,那么它可能是传递给它的一些空值。
    • 是的,我把它放在了 chat() 里面。 index.html 正在从我开始用户输入的地方工作
    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 2021-08-18
    • 2020-09-16
    • 2011-10-26
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多