elasticsearch,flask,Python,Html,elasticsearch,Flask" /> elasticsearch,flask,Python,Html,elasticsearch,Flask" />

Python 为从数据库检索的数据创建动态html链接

Python 为从数据库检索的数据创建动态html链接,python,html,elasticsearch,flask,Python,Html,elasticsearch,Flask,我正在使用flask和html制作一个搜索引擎,同时使用elasticsearch进行搜索功能。 这是我的app.py文件 from flask import Flask, render_template, request from elasticsearch import Elasticsearch app = Flask(__name__) es = Elasticsearch() @app.route('/',methods=["GET","POST"]) def index():

我正在使用flask和html制作一个搜索引擎,同时使用elasticsearch进行搜索功能。
这是我的app.py文件

from flask import Flask, render_template, request
from elasticsearch import Elasticsearch

app = Flask(__name__)
es = Elasticsearch()
@app.route('/',methods=["GET","POST"])
def index():
    q = request.form.get("q")
    if q is not None:
        resp = es.search(index='aman', body={"query": {"match":{"NAME": q}}})
        return render_template('index1.html', q=q, response=resp["hits"]["hits"])
    else:
        return render_template('index1.html')

if __name__ =="__main__":
    app.run(debug=True)
这是我的index.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/" method="post" autocomplete="off">
        <label for="q"> search here : </label><input type="text" name="q" id="q" value="{{q}}">
        <input type="submit" value="search">
    {{response}}

</form>
</body>
</html>

标题
在此处搜索:
{{response}}
我的数据以json格式存储。因此,在搜索json时,将检索格式化的数据。由于可以根据查询一次检索多个文档,因此在单个页面上看起来很破旧。 所以我尝试通过制作html链接和使用html中的有序列表只显示数据的名称来使它更干净。为此,我将index.html中的{{response}}替换为:

    <ol>
        {% for resp in response %}
        <li><a href="{{resp['_source'].url}}"> {{resp._source.NAME}}</a></li>
        {% endfor %}
    </ol>

{resp在响应中的百分比为%}
  • {%endfor%}
    现在我得到了所需格式的数据,即。 这个问题是,理想情况下,单击url链接时,我应该获得与该url关联的数据。但它不会返回任何东西

    在服务器上找不到请求的URL

    这个错误非常明显,因为您在
    /
    只有一条路由。你可以用两种方法解决这个问题。您可以添加添加单个项目视图的路线,也可以使项目在同一页面中可折叠。我认为最好添加可折叠的项目,这样您就不必再次点击弹性搜索

    示例代码,显示名称和内容为的卡片。您可以根据自己的需要进行自定义。更多关于折叠卡片的信息

    
    {resp在响应中的百分比为%}
    {{resp._source.NAME}
    {{resp.\u source.ABOUT}
    {%endfor%}
    
    resp[''u source']是否有url属性?我在图像中看不到它?@Srikanth否。它没有url属性。如果从上面提到的代码中删除.url,单击链接时会出现以下错误在服务器上找不到请求的URL。``这有帮助。谢谢你,先生!
    <ol>
        <div class="accordion" id="accordionExample">
            <div class="card">
            {% for resp in response %}
                <div class="card-header" id="heading{{ loop.index }}">
                    <h2 class="mb-{{ loop.index }}">
                        <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse{{ loop.index }}" aria-expanded="true" aria-controls="collapse {{ loop.index }}">
                            {{resp._source.NAME}}
                        </button>
                    </h2>
                </div>
    
                <div id="collapse {{loop.index}}" class="collapse show" aria-labelledby="heading{{loop.index}}" data-parent="#accordionExample">
                    <div class="card-body">
                        {{resp._source.ABOUT}}
                    </div>
                </div>
            {% endfor %}
            </div>
        </div>
    </ol>