0

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

0

1 Answer 1

2

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">
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! I really can't see how I missed that, I knew it was something small. Thank you tons!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.