首页 > 解决方案 > 我的烧瓶应用程序中的错误“视图函数未返回有效响应。”在哪里?

问题描述

错误

(app) E:\skripsi_app>python app.py
 * Serving Flask app "app" (lazy loading)
* Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [03/Mar/2019 22:17:11] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Mar/2019 22:17:16] "POST / HTTP/1.1" 200 -
[2019-03-03 22:17:17,884] ERROR in app: Exception on /dataset [GET]
Traceback (most recent call last):
  File "E:\skripsi_app\app\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "E:\skripsi_app\app\lib\site-packages\flask\app.py", line 1816, in full_dispatch_request
    return self.finalize_request(rv)
  File "E:\skripsi_app\app\lib\site-packages\flask\app.py", line 1831, in finalize_request
    response = self.make_response(rv)
File "E:\skripsi_app\app\lib\site-packages\flask\app.py", line 1957, 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 - - [03/Mar/2019 22:17:17] "GET /dataset HTTP/1.1" 500 -

app.py 中的数据集函数

@app.route('/dataset',methods=['GET','POST'])
def dataset():
if request.method == 'POST':

    file = request.files['file']

    if 'file' not in request.files:
        return redirect(request.url)

    if file.filename == '':
        return redirect(request.url)

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return render_template('dataset.html')

在 HTML 中

<strong>Upload Data</strong>
      <br>
      <form method="POST" enctype="multipart/form-data">
      <input type="file" name="file">
      <button type="submit" value="Upload File" class="btn btn-primary">Upload</button>
      </form>

起初,我添加了功能datasetapp.py运行它。这个应用程序没问题,它适用于有效负载 csv,但是当我单击另一个链接chart.html(例如,返回页面dataset.html)时,会发生错误。请帮我解决这个问题

标签: pythonmysqlcsvflask

解决方案


正如我在评论部分提到的:

当您的 if 语句均未满足时, dataset 函数返回 None ,这不是有效的响应。只需在数据集函数的末尾添加默认行为。也许应该是return render_template('dataset.html'),或者return render_template('error_page.html')如果您将其视为错误/不需要的行为。


推荐阅读