'dict'对象不支持索引python flask

时间:2018-07-06 08:14:24

标签: python sqlalchemy

我正在尝试制作一个具有用于搜索postgres表的搜索按钮的网络应用。

我遇到了错误:

  

TypeError:“ dict”对象不支持索引

我的代码如下:

  

app.py

from flask import Flask, render_template, request
from sqlalchemy import create_engine

app = Flask(__name__)

db_string = "postgres://xi:x@x:5432/xx"

db = create_engine(db_string)

@app.route('/', methods=['GET', 'POST'])
def homepage():
    if request.method == 'POST':
        jn = request.form['jobnumber']
        result_set = db.execute("SELECT cost FROM public.options where optionno = (f'%jn%')").fetchall()
        return render_template('main.html', test=result_set, jn=jn)
    else:
        return render_template('main.html')

    if __name__ == "__main__":
        app.run(debug=True)

我的HTML是:

  

main.html

<!DOCTYPE html>
<html lang="en">
<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="textbox" placeholder="jobnumber">
</form>

<table> 

<td>
       {{test}}

</td>


</table>

</body>
</html>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

SQLAlchemy具有文本功能,可将字符串转换为sqlalchemy文本对象,该对象似乎为您正确地转义了SQL。

您需要导入 “来自sqlalchemy导入文本”

您需要从查询中删除f并将 tooltip: { dateTimeLabelFormats: { day: '%b %e, %Y' } }, 更改为Mar 01, 2000=

like