有没有办法从HTML获取用户输入,并使用python将输入运行到SQL数据库?输入是否需要解析?我希望用户能够键入商店名称,并让它返回相关的行
def search():
store_search = request.form.get("store")
if request.method == "POST":
if not store_search:
return "please type in a store!"
else:
c = conn.cursor()
c.execute("SELECT * FROM stores WHERE store_name= 'store_search'")
rows = c.fetchall()
for eachRow in rows:
return row
else:
return render_template("search.html")
HTML:
{% extends "main_page.html" %}
{% block title %}
Search
{% endblock %}
{% block main %}
<form action="{{ url_for('search') }}" method="post">
<fieldset>
<div class="form-group">
<input autocomplete="off" autofocus class="form-control"
name="store" placeholder="store" type="text"/>
</div>
<div class="form-group">
<button class="btn btn-default" type="submit">search</button>
</div>
<div class="page-header">
<h1>{{ store }}</h1>
</div>
</fieldset>
</form>
{% endblock %}
答案 0 :(得分:0)
您可以在RootViewController
中创建一个简单的应用,用于接收用户输入并扫描flask
中SELECT
查询返回的项目:
首先,创建用户表单。您可以将sqlite3
与jquery一起用于动态响应:
在ajax
:
search.html
在<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<input type='text' name='query' id='query'>
<button type='button' id='search'>Search</button>
<div id='results'></div>
</body>
<script>
$(document).ready(function() {
$('#search').click(function() {
var text = $('#query').val();
$.ajax({
url: "/search",
type: "get",
data: {query: text},
success: function(response) {
$("#results").html(response.html);
},
error: function(xhr) {
//Do Something to handle error
}
});
});
});
</script>
</html>
app.py