This question already has answers here:
Flask view return error “View function did not return a response”
(2个答案)
2个月前关闭。
我的 flask 和elasticsearch出现问题,我只想尝试获取索引内容并将其显示在表中,但是这发生了吗?
这是我的代码:
app.py
elastic.html:
你们能告诉我怎么了吗?它是我的代码还是我的elasticsearch错误?我调试app.py但无法访问url时没有错误
(2个答案)
2个月前关闭。
我的 flask 和elasticsearch出现问题,我只想尝试获取索引内容并将其显示在表中,但是这发生了吗?
这是我的代码:
app.py
from flask import Flask,request,redirect,render_template,jsonify,json
from elasticsearch import Elasticsearch
es = Elasticsearch()
app = Flask(__name__)
@app.route('/edan',methods=['GET','POST'])
def home():
if request.method == 'POST':
index = es.indices.get('*')
valueindex = request.form['index1']
resp2 = []
key_list = []
data = es.search(index=valueindex, body={"query": {"match_all": {}}})
for a in data["hits"]["hits"]:
dict = {}
for key in a["_source"].keys():
if key not in key_list:
key_list.append(key)
dict[key] = a["_source"][key]
resp2.append(dict)
return render_template('elastic.html',es=es,resp2=resp2,key_list= key_list,index=index)
if __name__ == "__main__":
app.run(debug=True)
elastic.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Elastic GUI</title>
</head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<body>
<form method="post">
<br>
<div class="container" align="center">
<td>Select Index :</td>
<select name="index1">
{% for index1 in index %}
<option>{{index1}}</option>
{% endfor %}
</select>
<br><br>
{% if index1 == index1 %}
<table border="1px" class="table table-dark" align="center">
<tr>
{% for key in key_list %}
<th>{{key}}</th>
{% endfor %}
</tr>
{% for row2 in resp2 %}
<tr>
{% for key in key_list %}
<td>{{row2[key]}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% else %}
<table border="1px">
</table>
{% endif %}
<br>
<button type="submit" class="btn btn-primary">Cari</button>
</div>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
</body>
</html>
你们能告诉我怎么了吗?它是我的代码还是我的elasticsearch错误?我调试app.py但无法访问url时没有错误
最佳答案
return语句似乎在for循环内。您可以取消缩进(删除该行上的制表符间距)并再次检查吗?
万一该请求不是POST请求,您可能还需要else语句,因为在此 View 函数上同时允许GET和POST方法。