Issue
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!!!
Solution
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">
Answered By - Epsi95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.