Cactix
Cactix

Reputation: 45

Python/Flask - Trying to take user input from one page, and display on another

So, I've seen a few posts on this, but can't seem to get this to work.

Basically, I have a two page site (home.html and result.html). The home.html has a user input for a name and submit button, and the result.html should display that name once submitted.

The goal is for a name to be input into the name field on the home.html page, and once submitted, the result page would load with the name being displayed. Currently, when submitted, only None shows.

Here are the files below:

home.html

{% extends 'base.html' %}
{% block head %}
<title>Home Page</title>
{% endblock %}
{% block body %}

  <div class="content-container">
    <form action="{{ url_for('result') }}" method="post">
      <h2>Input your name:</h2>
      <input type="text" class="name-input name mb-3" id="name">
      <input class="btn btn-outline-primary" type="submit" value="Submit">
    </form>
  </div>

{% endblock %}

result.html

{% extends 'base.html' %}
{% block head %}
<title>Result Page</title>
{% endblock %}
{% block body %}

  <div class="content-container">
    <h2>{{ name }}</h2>
  </div>

{% endblock %}

main.py

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

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        name = request.form["name"]
        return redirect(url_for('result', name=name))
    return render_template('home.html')


@app.route("/result", methods=['GET', 'POST'])
def result():
    name = request.form.get('name')
    return render_template("result.html", name=name)


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

I'm 99.9% sure that what I'm missing is small, and once pointed out, I'll feel bad for missing it. Regardless, I'm seeking help from the high counsel, PLEASE HELP!!! TIA!!!

Upvotes: 0

Views: 2405

Answers (1)

Epsi95
Epsi95

Reputation: 9047

input field should have an attribute called name which you refer as request.form[name]. So it should be

<input type="text" class="name-input name mb-3" id="name" name="name">

Upvotes: 2

Related Questions