Home > Mobile >  simple flask application form, form page not functioning
simple flask application form, form page not functioning

Time:11-26

form page not displaying response,

I left off some tags that were supposed to be closed. I changed that already. Basically, once i fill out the form and click on the submit button it should take me to the display page where it displays a string such as " Hello" string "thank you for submitting!"

main.py code


#import the flask module 
from flask import Flask, render_template, request,url_for

app = Flask(__name__)

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


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

@app.route("/form", methods = ['GET','POST'])
def form():
    #get the method of the post and the method of the get 
    if request.method == "POST"  and request.form.get('submit'):
        string = request.form.get('name')
        feedback = "Hello"   string   "\n Thank you for submiting!!"
        return render_template('display.html').format(feedback = feedback)
    else:
        return render_template('form.html').format(feedback = "")
    
   
    
    
#run the program 

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

form.html

<!DOCTYPE  html>

<html>

<head> 
    <title>World Cup</title>
</head>

<body>

    <h1> What team are you going to support? </h1>
    <h2> Please fill in the information in the bottom</h2>
    <br> <br>

    <nav> <a href = "{{url_for('home')}}">Home</a> &nbsp; &nbsp; &nbsp; 
    <a href = "{{url_for('teams')}}">Teams</a> &nbsp; &nbsp; &nbsp; 
    <a href = "{{url_for('form')}}">Form</a>  &nbsp; &nbsp; &nbsp; 
    </nav>
    <br> <br>


    <form method = "post">
        <label> Name:
        <input type ="text" name = "name"></label>
        <br> <br>

        <label> Comments:
        <input type = "text" name = "name"> </label>
        <br> <br>

        <input type = "submit" value = "Submit">
        <input type = "reset">
    </form>



</body>



</html>

display.html code

<!DOCTYPE  html>

<html>

<head> 
    <title>World Cup</title>
</head>

<body>

    <nav> <a href = "{{url_for('home')}}">Home</a> &nbsp; &nbsp; &nbsp; 
    <a href = "{{url_for('teams')}}">Teams</a> &nbsp; &nbsp; &nbsp; 
    <a href = "{{url_for('form')}}">Form</a>  &nbsp; &nbsp; &nbsp; 
    </nav>
 
    <p>
    {{feedback}}
    </p>

</body>

</html>

CodePudding user response:

If everything is ok with you except that feedback is not displaying then do this:

def form():
#get the method of the post and the method of the get 
    if request.method == "POST"  and request.form.get('submit'):
        string = request.form.get('name')
        feedback = "Hello"   string   "\n Thank you for submiting!!"
        return render_template('display.html', feedback=feedback)
    else:
        feedback = ... # define feedback here
        return render_template('form.html', feedback=feedback)

CodePudding user response:

As mentioned in the Comments with the Code you provide the if Block never gets true. Because inside the Form Data there is no thing called submit. To solve this your form should look like this:

<!DOCTYPE  html>
<html>
<head> 
    <title>World Cup</title>
</head>
<body>
    <h1> What team are you going to support? </h1>
    <h2> Please fill in the information in the bottom</h2>
    <br> <br>

    <nav> <a href = "{{url_for('home')}}">Home</a> &nbsp; &nbsp; &nbsp; 
    <a href = "{{url_for('teams')}}">Teams</a> &nbsp; &nbsp; &nbsp; 
    <a href = "{{url_for('form')}}">Form</a>  &nbsp; &nbsp; &nbsp; 
    </nav>
    <br> <br>
    <form method = "post">
        <label> Name:
        <input type ="text" name = "name"></label>
        <br> <br>

        <label> Comments:
        <input type = "text" name = "Comments"> </label>
        <br> <br>

        <input type = "submit" name="submit" value = "Submit">
        <input type = "reset">
    </form>
</body>
</html>

So i added the name Attribute to the Submit Button. I also changed the name Attribute of the Comment button because it is identically with the name of the Name Field.

The second Problem is the call of render_template. To pass Variables to the Form you have to put them into the render_template call.

This is how your route should look like:

@app.route("/form", methods=["GET", "POST"])
def form():
    # get the method of the post and the method of the get
    if request.method == "POST" and request.form.get("submit"):
        string = request.form.get("name")
        feedback = "Hello"   string   "\n Thank you for submiting!!"
        return render_template("display.html", feedback=feedback)
    else:
        return render_template("form.html")
  • Related