0

without a redirect everything works fine, but with this redirect, nothing seems to be working. I've tried updating the code, tried forcing a POST request, check tons of SO posts but found no solution that works for me.

Using Python 3.7 Flask Code:

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/', methods=['POST'])
def num_of_students():
    num = request.form['text']
    return redirect(url_for('test123',num=num))

@app.route('/test123')
def test123():
    return render_template("test.html")



if __name__ == "__main__":
    app.run(debug=True)

HTML Code Index:

<!doctype html>
<title>Rubric</title>

<label>Welcome to the Autograding Rubric</label>
<p>Please enter the number of students.</p>
<form method="POST">
    <input type="text", name="text">
    <input type="submit">
</form>        
</html>

Test:

<!DOCTYPE html>
<title>Test</title>
<p>Test Var: {{ num }}</p>
RWalling21
  • 159
  • 1
  • 1
  • 9

1 Answers1

1

The template isn't seeing num because it isn't being pass in via render_template. Try

@app.route('/test123')
def test123():
    num = request.form["text"]
    return render_template("test.html", num=num)

Update: I missed that you were reusing '/' as a route. Flask won't be happy with that. Instead, try something like

@app.route('/', methods=['GET', 'POST'])
@def index():
    if request.method == 'GET':
        return render_template("index.html")
    num = request.form['text']
    return redirect(url_for('test123'), num=num)
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46