【发布时间】:2021-03-23 16:32:02
【问题描述】:
问题描述:我有一个存储在 Sqlite 数据库中的球员姓名列表,位于球员表。
有一个搜索按钮。现在,当我搜索特定玩家Nick 时,在这种情况下971 中,我得到了DB 中存在的玩家的真实数量。我的每页显示是10。在这个分页中,页面 nr 1 没问题,但只要我点击页面 2,它就会显示以下错误
我该如何解决这个问题?
werkzeug.exceptions.BadRequestKeyError
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'text'
这是我的base.html search
<form class="form-inline my-2 my-lg-0" action="/search" method="POST">
<input class="form-control mr-sm-2" placeholder="Search" aria-label="Search" type="text" name="text">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
这是app.py 的search 端点
@app.before_request
def before_request():
g.conn = sqlite3.connect(database)
g.conn.row_factory = sqlite3.Row
g.cur = g.conn.cursor()
@app.teardown_request
def teardown(error):
if hasattr(g, "conn"):
g.conn.close()
@app.route("/search", methods=['GET', 'POST'])
@print_exceptions
def search():
"""The function is used to test multi values url."""
sql = "select count(*) from player_details where player like (?);"
# this is where giving me the problem
args = request.form['text']
# each page is moving as if it's end point for search
g.cur.execute(sql, [args])
total = g.cur.fetchone()[0]
print(total)
page, per_page, offset = get_page_args()
sql = "select id, player, floor from player_details where player like (?) limit {}, {}"
print(sql.format(offset, per_page))
g.cur.execute(sql.format(offset, per_page), [args])
users = g.cur.fetchall()
pagination = get_pagination(
page=page,
per_page=per_page,
total=total,
record_name="users",
)
return render_template(
"index.html",
users=users,
page=page,
per_page=per_page,
pagination=pagination,
)
def get_css_framework():
return current_app.config.get("CSS_FRAMEWORK", "bootstrap4")
def get_link_size():
return current_app.config.get("LINK_SIZE", "sm")
def get_alignment():
return current_app.config.get("LINK_ALIGNMENT", "")
def show_single_page_or_not():
return current_app.config.get("SHOW_SINGLE_PAGE", False)
def get_pagination(**kwargs):
kwargs.setdefault("record_name", "records")
return Pagination(
css_framework=get_css_framework(),
link_size=get_link_size(),
alignment=get_alignment(),
show_single_page=show_single_page_or_not(),
**kwargs
)
更新
我对@987654341@ 功能进行了如下更改:
@app.route("/search/<name>/")
@app.route("/search/<name>")
@print_exceptions
def search(name):
"""The function is used to test multi values url."""
sql = "select count(*) from player_details where player like (?);"
args = ("%{}%".format(name),)
print(args)
# args = request.form.get("name")
# if not args:
# args = request.form['text']
# else:
# args = args
g.cur.execute(sql, args)
.....
....
现在它在 gui 中作为 API 调用工作......就像这样 http://localhost:5050/search/Alex 然后从那里开始分页很顺利。
但是现在按钮搜索不起作用......有什么办法可以将按钮和 API 调用一起添加?
【问题讨论】: