[英]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.