flask - 在烧瓶中使用搜索表单进行分页
问题描述
我正在尝试使用烧瓶对搜索选项进行分页。我的代码正在运行。但是当我像“tool1”一样搜索时,它会显示在三个页面上。
如果我点击第二页或下一页链接,那么它会用我的表格中总共 8 行的所有数据刷新我的页面。
单击任何页码时,搜索的数据应每页显示 2 条记录。
我必须在这里纠正什么?
主要的。py
from flask import Flask, render_template, request, jsonify, redirect, url_for
import flask
from flask_paginate import Pagination, get_page_parameter
import json
import dbConnect
import datetime
app = Flask(__name__)
app.secret_key = "logger@!#$%^"
@app.route("/home",methods=['GET','POST'])
def home():
toolName = None
if "toolname" in request.form:
toolName = request.form["toolname"]
solution_data = solution_result(toolName)
return render_template("home.html",solution_data=solution_data[0],pagination=solution_data[1])
def solution_result(toolName):
# Setting page, limit and offset variables
per_page = 2
page = request.args.get(get_page_parameter(), type=int, default=1)
offset = (page - 1) * per_page
cursor = dbConnect.ConnectDatabase()
# Executing a query to get the total number of products
if toolName:
total_sql = sql = "select tool from error_history where tool=%s"
cursor.execute(total_sql,(toolName))
total = cursor.fetchall()
else:
total_sql = sql = "select tool from error_history"
cursor.execute(total_sql)
total = cursor.fetchall()
# execute SQL query using execute() method.
if toolName:
sql = "select id, tool, error from error_history where tool=%s ORDER BY id DESC LIMIT %s OFFSET %s"
cursor.execute(sql,(toolName,per_page, offset))
else:
sql = "select id, tool, error from error_history ORDER BY id DESC LIMIT %s OFFSET %s"
cursor.execute(sql,(per_page, offset))
result = cursor.fetchall()
# Setting up the pagination variable, where you are using len(total) to set the total number of
# items available
pagination = Pagination(page=page, per_page=per_page, offset=offset, total=len(total),
record_name='products',css_framework='bootstrap3')
dbConnect.closeConnection()
return result, pagination
if __name__ == "__main__":
app.run(debug=True)
dbConnect.py
import pymysql
client = None
def ConnectDatabase():
try:
global client
# Open database connection
client = pymysql.connect("localhost","root","","solution")
# makes autocommit false
client.autocommit(False)
# prepare a cursor object using cursor() method
cursor = client.cursor()
#print("******** database connected ****")
return cursor
except:
print("******** database not connected ****")
def closeConnection():
global client
client.close()
if __name__ == "__main__":
ConnectDatabase()
solution.error_history 表
CREATE TABLE `error_history` (
`id` int(11) NOT NULL,
`tool` varchar(255) NOT NULL,
`error` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `error_history` (`id`, `tool`, `error`) VALUES
(1, 'tool1', 'error in toola'),
(2, 'tool2', 'error in toole'),
(3, 'tool3', 'error in place'),
(4, 'tool1', 'error in toolh'),
(5, 'tool1', 'error in toolo'),
(6, 'tool1', 'error in tool123'),
(7, 'tool1', 'error in toolxy'),
(8, 'tool2', 'error in toolxyz');
ALTER TABLE `error_history`
ADD PRIMARY KEY (`id`);
ALTER TABLE `error_history`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
主页.html
<!DOCTYPE html>
<html lang="en" dir="DataLogger">
<head>
<meta charset="utf-8">
<title>Data</title>
<style>
.pagination-page-info {
padding: .6em;
padding-left: 0;
width: 40em;
margin: .5em;
margin-left: 0;
font-size: 12px;
}
.pagination-page-info b {
color: black;
background: #6aa6ed;
padding-left: 2px;
padding: .1em .25em;
font-size: 150%;
}
</style>
</head>
<body>
<div class="wrapper">
{% block content %}
<div id="Sidebar">
</div>
<!-- Page Content Holder -->
<div id="content" >
<p class="" style="font-weight: bold;">Dashboard</p>
<div class="row">
<form name="searchForm" action="{{ url_for('home') }}" method="POST" >
<div class="form-group col-xs-10 col-sm-4 col-md-4 col-lg-4">
<input type="input" class="form-control" id="toolname" name="toolname" placeholder="Search tool"
value="" required>
</div>
<div class="form-group col-xs-10 col-sm-4 col-md-4 col-lg-4">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</form>
</div>
<div class="row col-xs-10 col-sm-4 col-md-4 col-lg-12">
{{ pagination.info }}
</div>
<div class="row table-responsive col-xs-10 col-sm-4 col-md-4 col-lg-12">
<table class="table table-bordered solutionTable ">
<thead class="">
<tr>
<th>Sl No.</th>
<th>Tool</th>
<th>error</th>
</tr>
</thead>
<tbody>
{% for row in solution_data %}
<tr>
<td>{{loop.index + (pagination.page - 1) * pagination.per_page }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="row col-xs-10 col-sm-4 col-md-4 col-lg-12">
{{ pagination.links }}
</div>
<!-- Page Content Holder -->
</div>
{% endblock %}
</body>
</html>
解决方案
推荐阅读
- javascript - 验证列表的父节点是否为“元素”类型的 DOM 节点
- dialogflow-es - 如何从自定义聊天 UI 向 DialogFlow 代理发送请求?
- amazon-s3 - S3 拒绝策略不适用于 IAM 角色
- authentication - 使用加密与解密的身份验证
- dart - 删除数组“distinct”中重合的项目
- html - 无序列表中的元素不占用其父容器的 100% 宽度
- c - 为什么通过 cmd 运行可执行文件不会调用 system() 调用?
- vuejs2 - Vue方法方法不使用if语句返回任何内容
- excel-formula - 如何在单个单元格中运行多个 if 语句
- c++ - 当一个函数/方法需要 * 或 &