【问题标题】:TypeError: The view function did not return a valid response [duplicate]TypeError:视图函数没有返回有效响应[重复]
【发布时间】:2020-09-16 03:58:46
【问题描述】:

我正在查看SO post,但答案无效。

这是我的烧瓶应用程序代码,HTML 中有一个按钮可以浏览 csv 文件。最终在 pandas 中处理 CSV 文件并打印在名为 kW 的列中找到的最大值

from flask import Flask, make_response, request
from werkzeug.utils import secure_filename
import pandas as pd

app = Flask(__name__)


@app.route('/')
def form():
    return """
        <html>
            <body>
                <h1>Analysis</h1>

                <form action="/transform" method="post" enctype="multipart/form-data">
                    <input type="file" name="data_file" />
                    <input type="submit" />
                </form>
            </body>
        </html>
    """

@app.route('/transform', methods=["POST"])
def transform_view():
    # data_file is the name of the file upload field
    f = request.files['data_file']

    # for security - stops a hacker e.g. trying to overwrite system files
    filename = secure_filename(f.filename)

    # save a copy of the uploaded file
    f.save(filename)

    # And then use it ...
    df = pd.read_csv(filename, index_col='Date', parse_dates=True)

    maxy = df.kW.max()
    print(maxy)

这是simple.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% for table in tables %}
            {{titles[loop.index]}}
            {{ table|safe }}
{% endfor %}
</body>
</html>

非常感谢任何提示...当我运行代码(烧瓶工作)时,在浏览过程中选择文件时会引发错误。

TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

完整的追溯:

File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2309, in __call__

return self.wsgi_app(environ, start_response)

File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2295, in wsgi_app

response = self.handle_exception(e)

File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1741, in handle_exception

reraise(exc_type, exc_value, tb)

File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\_compat.py", line 35, in reraise

raise value

File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2292, in wsgi_app

response = self.full_dispatch_request()

File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1816, in full_dispatch_request

return self.finalize_request(rv)

File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1831, in finalize_request

 [Open an interactive python shell in this frame] response = self.make_response(rv)

File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1957, in make_response

'The view function did not return a valid response. The'

【问题讨论】:

    标签: python pandas flask


    【解决方案1】:

    发生这种情况是因为您的路线 \transform 确实返回了有效响应。这个路由必须有一个return语句。我假设你想在调用这个路由时加载simple.html,你可以做以下事情:

    1. 导入render_template:
    from flask import render_template
    
    1. 在路由中使用 return 语句,如下所示:
    return render_template('simple.html')
    

    或者只是为了虚拟目的,您希望这条路线执行没有任何错误,然后您可以返回如下:

    return 'Transformed!'
    

    注意:这取决于执行此路由时您实际想要返回的内容或想要渲染的内容。您可以阅读文档。

    【讨论】:

    • 感谢帮助我学习 web 端...当我将 return render_template 添加到 app.route('/transform', methods=["POST"]) 时会引发错误...jinja2.exceptions.TemplateNotFound: simple.html
    • 是否可以在浏览器中查看print(maxy)?非常感谢您发布我的问题
    • 哦,不用担心,我们在这里为您提供帮助。 simple.html 必须在项目根目录的templates 目录下。可以参考this
    • 是的,您可以轻松地在网络浏览器上显示此内容,只需在路线中使用return maxy
    • 另外,你应该确保,返回类型必须是字符串、dict、元组、响应实例或WSGI callable,否则会抛出异常。
    猜你喜欢
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 2019-09-07
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多