我正在尝试创建一个简单的搜索工具,使用Flask,Jinja和SQLAlchemy搜索现有的Postgres表。一切正常,除了我搜索时,页面上显示的结果是这样的:
请注意,如果我仅使用Postgres / pgadmin进行了相同的搜索,将会圈出我要圈出的位。它返回多个随机结果。以下是使用Pgadmin返回的内容:
有任何想法吗?我的代码如下。
应用程式
from flask import Flask, render_template, request
from sqlalchemy import create_engine
from sqlalchemy import text
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='xxx://xxx:xxx@xxx:xxx/xxx'
engine = create_engine('postgresql+psycopg2://xxx:xxx@xxxr:xxx/xxx')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db=SQLAlchemy(app)
@app.route('/', methods=['GET', 'POST'])
def homepage():
if request.method == 'POST':
jn = request.form['jobnumber']
rp = db.session.execute(text("SELECT cost FROM public.options where cast(optionno AS VARCHAR) like :jn"), {"jn": f"%{jn}%"})
result_set = rp.fetchall()
return render_template('main.html', result_set=result_set, jn=jn)
else:
return render_template('main.html')
if __name__ == "__main__":
app.run(debug=True)
Main.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>xxx</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
</head>
<body>
<p>xxx</p>
<form method="POST" id="jobnumber">
<input name="jobnumber" type="text" placeholder="jobnumber">
</form>
<table>
<td>
<h1> Results</h1>
<p>{{result_set}}</p>
</td>
</table>
</body>
</html>
如何获取仅显示与PGAdmin相同的内容?
喵喵时光机
相关分类