1

I'm new to Flask and followed this walkthrough but am running into an issue. I want a user to be able to input text in a text field on the index.html page, hit the submit button, and take them to trader.html page. I'm running into a 404 Page Not Found error with my below code when the button is hit, but I'm not sure where I'm going wrong? The URL bar shows the correct /trader.html address after the button is hit, but getting the 404 error page.

The 2 answers I found on SO for similar issues didn't work here as they mentioned that there needed to be a separate app.route to navigate them to the correct second page, but I already have that set up in my code???

app.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')

index.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>

trader.html

<h1 style="color: #4485b8;">{{inputted_stock}} trader!</h1>
<p>Begin testing...</p>
wildcat89
  • 1,159
  • 16
  • 47

1 Answers1

2

The path for your trader_page handler is just "/trader". So that's what you need to use in the form action.

<form action="/trader" method="POST">

Even better, use url_for to generate the URL:

<form action="{{ url_for('trader_page') }}" method="POST">
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895