错误请求 - 浏览器(或代理)发送了该服务器无法理解的请求
Bad Request - The browser (or proxy) sent a request that this server could not understand
我正在尝试创建一个 Flask 应用程序,我在网页上有一个文本框。当按下提交时,它会搜索在 postgres 数据库 table.
中输入到文本框中的内容
我收到以下错误:
Bad Request The browser (or proxy) sent a request that this server could not
understand."
我的代码如下:
app.py
from flask import Flask, render_template, request
from sqlalchemy import create_engine
app = Flask(__name__)
app.config['DEBUG']
db_string = "postgres://xx:xx@xx:5432/xx"
db = create_engine(db_string)
@app.route('/', methods=['GET', 'POST'])
def homepage():
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)
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">
<input name="jobnumber" type="submit" placeholder="jn">
</form>
<table>
<td>
{{test}}
</td>
</table>
</body>
</html>
我敢肯定这真的很简单,可以解决这个问题,但我正在努力,所以任何帮助都将不胜感激。
由于您的 homepage
函数接收到 GET 和 POST 请求,您需要分别处理每种情况。当您收到 GET 请求时,您没有 request.form
。
@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')
请注意,将用户的输入直接放入您的 SQL 查询而不对其进行清理是很危险的,因为这会使您的应用程序受到 SQL injection 攻击。
我正在尝试创建一个 Flask 应用程序,我在网页上有一个文本框。当按下提交时,它会搜索在 postgres 数据库 table.
中输入到文本框中的内容我收到以下错误:
Bad Request The browser (or proxy) sent a request that this server could not understand."
我的代码如下:
app.py
from flask import Flask, render_template, request
from sqlalchemy import create_engine
app = Flask(__name__)
app.config['DEBUG']
db_string = "postgres://xx:xx@xx:5432/xx"
db = create_engine(db_string)
@app.route('/', methods=['GET', 'POST'])
def homepage():
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)
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">
<input name="jobnumber" type="submit" placeholder="jn">
</form>
<table>
<td>
{{test}}
</td>
</table>
</body>
</html>
我敢肯定这真的很简单,可以解决这个问题,但我正在努力,所以任何帮助都将不胜感激。
由于您的 homepage
函数接收到 GET 和 POST 请求,您需要分别处理每种情况。当您收到 GET 请求时,您没有 request.form
。
@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')
请注意,将用户的输入直接放入您的 SQL 查询而不对其进行清理是很危险的,因为这会使您的应用程序受到 SQL injection 攻击。