传递 Post 库存筛选器数据时出错
Error When Passing Post Data In Stock Screener
我正在尝试 select 股票 ID 和策略 ID 两个值,以便重定向到显示数据的页面,但是,当我尝试重定向到我的策略时,出现此错误:
{"detail":[{"loc":["body","stock_id"],"msg":"field
required","type":"value_error.missing"}]}
这是我设置主程序的方式:
@app.post("/apply_strategy")
def apply_strategy(strategy_id:int = Form(...), stock_id:int = Form(...)):
print(stock_id, strategy_id)
connect = sqlite3.connect(config.DB_FILE)
cursor = connect.cursor()
print('Test here')
cursor.execute("""
INSERT INTO stock_strategy (stock_id, strategy_id) VALUES (?, ?)
""", (stock_id, strategy_id))
connect.commit()
return RedirectResponse(url=f"/strategy/{strategy_id}", status_code=303)
@app.get("/strategy/{strategy_id}")
def strategy(request: Request, strategy_id):
print('test')
connect = sqlite3.connect(config.DB_FILE)
connect.row_factory = sqlite3.Row
cursor = connect.cursor()
cursor.execute("""
SELECT id, name
FROM strategy
WHERE id = ?
""", (strategy_id,))
strategy = cursor.fetchone()
cursor.execute("""
SELECT symbol, name
FROM stock JOIN stock_strategy on stock_strategy.stock_id = stock.id
WHERE strategy_id = ?
""", (strategy_id,))
stocks = cursor.fetchall()
return templates.TemplateResponse("strategy.html", {"request": request, "stocks": stocks, "strategy": strategy})
我正在将数据从 data_strategies 传递到我的策略页面:
<form method="post" action ="/apply_strategy" >
<select name = "strategy_id">
{% for strategy in strategies %}
<option value="{{ strategy.id }}">{{ strategy.name }}</option>
{% endfor %}
</select>
<input type="text" value="{{ stock.id }}" name="{{ stock.id }}"/>
<input type="submit" value="Apply Strategy" />
</form>
提交表单后,我收到错误消息。谁能告诉我哪里出错了。
您的问题并非来自 RedirectResponse,您正在向后端发送带有错误参数的请求 API。
您的 from 中的名称字段应该是 stock_id
,因为您的后端需要一个带有两个键的 JSON 对象,并且这些键应该是您的端点参数的确切键。
<input type="text" value="{{ stock.id }}" name="stock_id"/>
<input type="submit" value="Apply Strategy" />
此外,您的 /strategy/{strategy_id}
端点还有另一个问题。你有一个路径参数,但你声明它时没有类型注释。
@app.get("/strategy/{strategy_id}")
def strategy(request: Request, strategy_id: int):
...
您应该键入注释您的每个参数,否则,FastAPI 假定它是一个主体参数。
我正在尝试 select 股票 ID 和策略 ID 两个值,以便重定向到显示数据的页面,但是,当我尝试重定向到我的策略时,出现此错误:
{"detail":[{"loc":["body","stock_id"],"msg":"field required","type":"value_error.missing"}]}
这是我设置主程序的方式:
@app.post("/apply_strategy")
def apply_strategy(strategy_id:int = Form(...), stock_id:int = Form(...)):
print(stock_id, strategy_id)
connect = sqlite3.connect(config.DB_FILE)
cursor = connect.cursor()
print('Test here')
cursor.execute("""
INSERT INTO stock_strategy (stock_id, strategy_id) VALUES (?, ?)
""", (stock_id, strategy_id))
connect.commit()
return RedirectResponse(url=f"/strategy/{strategy_id}", status_code=303)
@app.get("/strategy/{strategy_id}")
def strategy(request: Request, strategy_id):
print('test')
connect = sqlite3.connect(config.DB_FILE)
connect.row_factory = sqlite3.Row
cursor = connect.cursor()
cursor.execute("""
SELECT id, name
FROM strategy
WHERE id = ?
""", (strategy_id,))
strategy = cursor.fetchone()
cursor.execute("""
SELECT symbol, name
FROM stock JOIN stock_strategy on stock_strategy.stock_id = stock.id
WHERE strategy_id = ?
""", (strategy_id,))
stocks = cursor.fetchall()
return templates.TemplateResponse("strategy.html", {"request": request, "stocks": stocks, "strategy": strategy})
我正在将数据从 data_strategies 传递到我的策略页面:
<form method="post" action ="/apply_strategy" >
<select name = "strategy_id">
{% for strategy in strategies %}
<option value="{{ strategy.id }}">{{ strategy.name }}</option>
{% endfor %}
</select>
<input type="text" value="{{ stock.id }}" name="{{ stock.id }}"/>
<input type="submit" value="Apply Strategy" />
</form>
提交表单后,我收到错误消息。谁能告诉我哪里出错了。
您的问题并非来自 RedirectResponse,您正在向后端发送带有错误参数的请求 API。
您的 from 中的名称字段应该是 stock_id
,因为您的后端需要一个带有两个键的 JSON 对象,并且这些键应该是您的端点参数的确切键。
<input type="text" value="{{ stock.id }}" name="stock_id"/>
<input type="submit" value="Apply Strategy" />
此外,您的 /strategy/{strategy_id}
端点还有另一个问题。你有一个路径参数,但你声明它时没有类型注释。
@app.get("/strategy/{strategy_id}")
def strategy(request: Request, strategy_id: int):
...
您应该键入注释您的每个参数,否则,FastAPI 假定它是一个主体参数。