Python 在Stock Screener中传递Post数据时出错
我尝试选择两个值stock id和strategy id,以便重定向到显示数据的页面,但当我尝试重定向到策略时,出现以下错误: {“详细信息”:[{“loc”:[“正文”,“库存id”],“消息”:“字段” 必需“,”类型“:“值\错误.缺少”}]} 以下是我如何设置我的主要功能:Python 在Stock Screener中传递Post数据时出错,python,html,fastapi,Python,Html,Fastapi,我尝试选择两个值stock id和strategy id,以便重定向到显示数据的页面,但当我尝试重定向到策略时,出现以下错误: {“详细信息”:[{“loc”:[“正文”,“库存id”],“消息”:“字段” 必需“,”类型“:“值\错误.缺少”}]} 以下是我如何设置我的主要功能: @app.post("/apply_strategy") def apply_strategy(strategy_id:int = Form(...), stock_id:int = Form(.
@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_策略传递到我的策略页面:
<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>
{strategies%%中的策略为%s}
{{strategy.name}
{%endfor%}
一旦提交表单,我就会得到错误信息。有人能告诉我哪里出了问题。您的问题不是来自重定向响应,您正在向后端API发送带有错误参数的请求 from中的name字段应该是
stock_id
,因为后端需要一个包含两个键的JSON对象,并且这些键应该是包含端点参数的精确键
另外,在/strategy/{strategy\u id}
端点中还有另一个问题。您有一个path参数,但声明它时没有类型注释
@app.get(“/strategy/{strategy\u id}”)
def策略(请求:请求,策略_id:int):
...
您应该为每个参数输入注释,否则,FastAPI会假定它是一个主体参数。您的问题不是来自重定向响应,而是您向后端API发送了一个带有错误参数的请求 from中的name字段应该是
stock_id
,因为后端需要一个包含两个键的JSON对象,并且这些键应该是包含端点参数的精确键
另外,在/strategy/{strategy\u id}
端点中还有另一个问题。您有一个path参数,但声明它时没有类型注释
@app.get(“/strategy/{strategy\u id}”)
def策略(请求:请求,策略_id:int):
...
您应该为每个参数键入注释,否则,FastAPI会假定它是一个主体参数。路由名称不一致。您的意思是什么?路由名称不一致。您的意思是什么?