Python 3 Flask - 404 Page Not Found 错误

我是 Flask 的新手,并遵循了这个演练,但遇到了一个问题。我希望用户能够在 index.html 页面上的文本字段中输入文本,点击提交按钮,然后将它们带到 trader.html 页面。当按下按钮时,我在下面的代码中遇到了 404 Page Not Found 错误,但我不确定我哪里出错了?点击按钮后,URL 栏显示正确的 /trader.html 地址,但出现 404 错误页面。


我在 SO 上找到的针对类似问题的 2 个答案在这里不起作用,因为他们提到需要有一个单独的 app.route 才能将它们导航到正确的第二页,但我已经在我的代码中进行了设置???


应用程序.py


from 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>


沧海一幻觉
浏览 154回答 1
1回答

慕盖茨4494581

您的 trader_page 处理程序的路径只是“/trader”。这就是您需要在表单操作中使用的内容。<form&nbsp;action="/trader"&nbsp;method="POST">更好url_for的是,用于生成 URL:<form&nbsp;action="{{&nbsp;url_for('trader_page')&nbsp;}}"&nbsp;method="POST">
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python