[英]Connect HTML page with Elasticsearch using Python flask
我是Web开发的新手。 我正在尝试创建一个将显示弹性搜索数据库索引的网页。 我正在使用python flask作为后端。 我看到html页面和python控制台显示索引。 但是我无法从HTML页面获取索引。
我不确定可能是什么问题
Python代码如下:
from flask import Flask,render_template, request
from elasticsearch import Elasticsearch
app = Flask(__name__)
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
doc1 = {"food": "Japanese", "spice_level": "moderate"}
doc2 = {"food": "Italian", "spice_level": "mild"}
doc3 = {"food": "Indian", "spice_level": "spicy"}
es.index(index="food", doc_type="spice_level", id=1, body=doc2)
resp = es.get(index="food", doc_type="spice_level", id=1)
print(resp)
@app.route('/')
def home():
return render_template('index.html')
app.route('/dashboard', methods=['GET', 'POST'])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
HTML代码如下:
<!DOCTYPE html>
<BODY bgcolor="cyan">
<form method="GET" action="/dashboard">
<center>
<H1>Database UI </H1> <br>
search here <input type = "text" name= "index" /> <br>
<input type = "submit">
</center>
</form>
</BODY>
</html>
每当我输入索引名称并单击“搜索”按钮时,页面就会显示以下错误:
在服务器上找不到请求的URL。 如果您手动输入网址,请检查拼写,然后重试。
那时我看不到任何其他错误,并且很难用很少的错误信息进行调试。
为什么您的/dashboard
返回404?
因为缺乏对响应的查看功能。
app.route('/dashboard', methods=['GET', 'POST'])
无效。
如何访问elascticsearch的/dashboard
?
就您而言,最简单的方法是修改index.html
<!DOCTYPE html>
<BODY bgcolor="cyan">
<form method="POST" action="http://localhost:9200/dashboard">
<center>
<H1>Database UI </H1> <br>
search here <input type = "text" name= "index" /> <br>
<input type = "submit">
</center>
</form>
</BODY>
</html>
你可以在这里使用吗? 为了将数据从html解析为python代码,您需要在@ app.route内添加POST,如下所示:
@app.route("/", methods=['GET', 'POST'])
def home():
return render_template('index.html')
如果您想将数据插入index.html,可以在这里使用它:
somedata =“可变字符串”
render_template('index.html',somedata = somedata)
在index.html内做{{somedata}}
<!DOCTYPE html>
<BODY bgcolor="cyan">
<form method="POST" action="">
<center>
<H1>Database UI </H1> <br>
<!-- it will show (variable string) -->
{{ somedata }}
search here <input type = "text" name= "index" /> <br>
<input type = "submit">
</center>
</form>
</BODY>
</html>
快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.