我是 Flask 的新手,并遵循了这个演练,但遇到了一个问题。我希望用户能够在 index.html 页面上的文本字段中输入文本,点击提交按钮,然后将它们带到 trader.html 页面。当按下按钮时,我在下面的代码中遇到了 404 Page Not Found 错误,但我不确定我哪里出错了?点击按钮后,URL 栏显示正确的 /trader.html 地址,但出现 404 错误页面。我在 SO 上找到的针对类似问题的 2 个答案在这里不起作用,因为他们提到需要有一个单独的 app.route 才能将它们导航到正确的第二页,但我已经在我的代码中进行了设置???应用程序.pyfrom flask import Flask, request, jsonify, render_template@app.route('/', methods = ['GET'])def hello(): return render_template('index.html')@app.route('/trader', methods = ['POST'])def trader_page(): the_stock=request.form['inputted_stock'] return render_template('trader.html')索引.html<h1 style="color: #4485b8;">Welcome to the live stock trader!</h1><p>Select a stock ticker below to begin testing:</p><form action="/trader.html" method="POST"><option value="AAPL">AAPL</option><option value="ABBV">ABBV</option><option value="ABT">ABT</option></select><br /><br /> <table><tr><td>Enter Stock Ticker</td><td><input type="text" name="inputted_stock" /></td></tr></table><input type="submit" value="Submit"/></form><p></p><table class="editorDemoTable" style="vertical-align: top;"><thead></thead></table>交易者.html<h1 style="color: #4485b8;">{{inputted_stock}} trader!</h1><p>Begin testing...</p>
1 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
您的 trader_page 处理程序的路径只是“/trader”。这就是您需要在表单操作中使用的内容。
<form action="/trader" method="POST">
更好url_for的是,用于生成 URL:
<form action="{{ url_for('trader_page') }}" method="POST">添加回答
举报
0/150
提交
取消
