python - 我的烧瓶应用程序中的错误“视图函数未返回有效响应。”在哪里?
问题描述
错误
(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>
起初,我添加了功能dataset
并app.py
运行它。这个应用程序没问题,它适用于有效负载 csv,但是当我单击另一个链接chart.html
(例如,返回页面dataset.html
)时,会发生错误。请帮我解决这个问题
解决方案
正如我在评论部分提到的:
当您的 if 语句均未满足时, dataset 函数返回 None ,这不是有效的响应。只需在数据集函数的末尾添加默认行为。也许应该是return render_template('dataset.html')
,或者return render_template('error_page.html')
如果您将其视为错误/不需要的行为。
推荐阅读
- python - 如何获取熊猫数据框的重复索引
- javascript - 如何删除不匹配的对象?
- sql - MariaDB If 语句与选择和更新查询
- python - 如何使 matplotlib 图表显示 X 轴上的所有值(在 tkinter 中)?
- python - 计算字符串列表中有多少双字符和三字符出现
- android - 无法从android中的应用程序解析库模块的方法
- google-cloud-platform - 无法在 GCP 平台上安装 Openshift
- sparql - SPARQL 和链接数据:获取外部属性的属性
- javascript - 如何在 React 的另一个类上调用 const?
- exception - System.Runtime.InteropServices.COMException:错误?