【问题标题】:How can I search in mongodb using an html page with button and using flask with python?如何使用带有按钮的 html 页面和使用带有 python 的烧瓶在 mongodb 中搜索?
【发布时间】:2023-03-15 01:40:01
【问题描述】:

您好,我是烧瓶和 html 的新手。 我有一个数据库,其中包含具有以下结构的文档集合(代表道路路线):

我希望能够通过如下结构的 html 页面查询我的 mongodb:

我在第一个字段中输入我的位置的位置(Lat1,Long1)在我的目的地的第二个字段插入(Lat2,Long2),如果它存在于数据库中,则通过搜索按钮将其打印在页面下方它会告诉我“路线不存在”。

我在其中创建按钮的简单而琐碎的 index.html 如下:

<body>
	<h1>Choose your route:</h1>
	<form action="/list" method="get" autocomplete="on">
	<td><input type="text" name="key" placeholder="Your Position" size="20" /></td>
	<td><input type="text" name="key" placeholder="Search Destination" size="20" /></td>
	<td><button type="submit">Search</button></td>
	<button type="Reset" value="Reset">Reset</button>

</form>
</body>

而带有flash的python代码如下:

@app.route("/list")
def lists ():
#Display the all Task
return render_template('index.html',h=heading)

@app.route("/search", methods=['GET'])
def search():
#Searching a Task with various references
Lat1=request.values.get("Lat1")
Long1=request.values.get("Long1")
Lat2=request.values.get("Lat2")
Long=request.values.get("Long2")
refer=request.values.get("refer")
#I make the comparison to understand if the route is present in the 
database.
if(Lat1 == "Lat1" and Long1 == "Long1" and Lat2 == "Lat2" and Long2 == 
"Long2"):
    test_l = tests.find({Lat1:ObjectId(Lat1)})
    test_l = tests.find({Long1:ObjectId(Long1)})
    test_l = tests.find({Lat2:ObjectId(Lat2)})
    test_l = tests.find({Long2:ObjectId(Long2)})
else:
    print("Route not present")
return 
render_template('searchlist.html',tests=test_l,t=title,h=heading)

searchlist.html 页面我不知道如何构建它。 所以我的问题是创建 searchlist.html 页面并更改烧瓶中的 python 代码以便能够搜索。 非常感谢。

【问题讨论】:

    标签: python html mongodb flask


    【解决方案1】:

    抱歉耽搁了 这是一种应该可行的方法。

    <body>
    <h1>Choose your route:</h1>
    <form action="/list" method="POST" autocomplete="on">
    <td><input type="text" name="mypos" placeholder="Your Position" size="20" /></td>
    <td><input type="text" name="yourpos" placeholder="Search Destination" size="20" /></td>
    <td><button type="submit">Search</button></td>
    <button type="Reset" value="Reset">Reset</button>
    

    和蟒蛇

    @app.route('/list', methods=['GET','POST'])
    def lists():
    if request.method == 'POST':
        my_position = request.form['mypos']#form input on initial position
        your_position = request.form['yourpos']#form input on proposed position
        pos = my_position.split(',')#split input into lat and long
        pos1 = your_position.split(',')
        lat1 = pos[0]#lattitude1
        long1 = pos[1]#longtitude 1
        lat2 = pos1[0]
        long2 = pos1[1]
        routes = mongo.db.routes#collection where routes are present
        check_db = routes.find()#check all documents in collection
        #return render_template('searchlist.html', pos=pos, pos1=pos1,lat1=lat1,long1=long1,lat2=lat2,long2=long2)
    
        for record in check_db:
            if (record['lat1'] == lat1 and record['lat2'] == lat2 and record['long1'] == long1 and record['long2'] == long2):
                return 'route found'
            else:
                return 'sorry route not found'
    
    return render_template('form.html')
    

    请联系我以进一步了解

    【讨论】:

      【解决方案2】:

      您可能希望将输入名称更改为唯一值,并将请求方法设置为 get 和 post。 使用 request.form['name'] 接收用户输入,将其作为查询传递给数据库并遍历所有值以找到匹配项。 我目前没有使用笔记本电脑。但我会发布一些你想要的代码。

      【讨论】:

      • 感谢您的建议。然而,正如我之前写的那样,我对 flash 和 html 都是新手。我想我理解您的建议,但我无法正确组合它们。
      • 非常感谢 elvis-chunks 最后我用你的代码解决了这个问题。
      猜你喜欢
      • 2021-09-22
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多