[英]Connect HTML page with Elasticsearch using Python flask
I am new to web development. 我是Web开发的新手。 I am trying to create a web page which will display index from elastic search database.
我正在尝试创建一个将显示弹性搜索数据库索引的网页。 I am using python flask for backend.
我正在使用python flask作为后端。 I see html page and python console shows index.
我看到html页面和python控制台显示索引。 But I am not able to fetch index from HTML page.
但是我无法从HTML页面获取索引。
I am not sure what could be the issue 我不确定可能是什么问题
Python code is as follows: 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 code is as follows: 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>
Whenever I type a index name and click on search button, page gives me error as : 每当我输入索引名称并单击“搜索”按钮时,页面就会显示以下错误:
The requested URL was not found on the server. 在服务器上找不到请求的URL。 If you entered the URL manually please check your spelling and try again.
如果您手动输入网址,请检查拼写,然后重试。
I cannot see any other error then this, and it's really difficult to debug with less information about error. 那时我看不到任何其他错误,并且很难用很少的错误信息进行调试。
why your /dashboard
return 404 ? 为什么您的
/dashboard
返回404?
because lack of view function to response. 因为缺乏对响应的查看功能。
app.route('/dashboard', methods=['GET', 'POST'])
is invalid. app.route('/dashboard', methods=['GET', 'POST'])
无效。
How to access /dashboard
of elascticsearch ? 如何访问elascticsearch的
/dashboard
?
In your case, the simplest way is modify the index.html 就您而言,最简单的方法是修改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>
can you use this here? 你可以在这里使用吗? for Parse data from html to python code you need to have POST inside @app.route like this:
为了将数据从html解析为python代码,您需要在@ app.route内添加POST,如下所示:
@app.route("/", methods=['GET', 'POST'])
def home():
return render_template('index.html')
if you want to pase data into index.html you can use this here: 如果您想将数据插入index.html,可以在这里使用它:
somedata = "variable string"
somedata =“可变字符串”
render_template('index.html', somedata=somedata)
render_template('index.html',somedata = somedata)
inside index.html do {{ 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>
happy codeing. 快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.