Home > Mobile >  Messing around with flask , my form page is not functioning the button is not working. It should dis
Messing around with flask , my form page is not functioning the button is not working. It should dis

Time:11-25

I have created a flask application of the soccer tournament. I am having issues with the form page, the submit button should display a text "Hello" string "for submitting!". I created a additional html page named display that displays this. Once, I filled out the form it did not do nothing.

#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()

Home html

<!DOCTYPE  html>

<html>

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

    <h1> Its World Cup Time</h1>

    <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> Lorem ipsum dolor sit amet. Qui nostrum culpa aut illo iusto 33 reiciendis odio sit inventore itaque ad voluptas perferendis est illum vitae. 
    Aut exercitationem quod ut laudantium internos ea consequatur mollitia a mollitia nisi. Sed repudiandae delectus in fugit repellendus et nihil internos id quisquam expedita est aperiam consequatur qui veniam molestias.
    Eos alias similique et officiis laudantium ut facere error At debitis reiciendis rem voluptatem voluptates!</p>



    <p> Et cumque sequi sed facilis neque non laborum molestiae non reiciendis blanditiis sed facere quia ut accusantium laboriosam ut galisum internos.
    Vel quis dolor ut mollitia beatae nam laborum modi. 
    33 quaerat harum sed enim modi vel maxime officiis quo ratione dolorum qui consequatur atque 33 quasi dolores.</p>


    <p> Ut voluptatem deleniti qui veritatis unde qui rerum possimus? Et dolore asperiores in rerum voluptatum ad quod autem.
    Aut repellat nihil ea harum reprehenderit et Quis necessitatibus. 
    Aut velit porro qui deserunt deserunt qui fugit recusandae ut architecto consequatur qui Quis quis!</p>


</body>

</html>

teams.html

<!DOCTYPE  html>

<html>
    <style>
table, th, td {
  border:1px solid black;
}
    </style>

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

    <h1> Favorites to win it </h1>

    <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> This years World Cup, 32 teams are in playing in the biggest stage of their country. Teams that left a huge shock that did not qualify were Italy, Colombia, Chile, and Denmark.
    The teams are divided into eight groups of four. When the tournament begins, each team competes in their group. The top tow teams in each group qualify into the knockout stages.  </p>



    <p> The team that has won the most World Cups is Brazil. Brazil is known for the style of plaay that is called JOGO Bonito. The key player to watch for in this Brazilian squad is 
    Neymar Jr. In the World Cup 2018, the France national team took it all. We got to see a new superstar Kylian Mbappe, who played for Monacco. During the 2018, Mbappe was explosive on the right and left wing. Taking on 
    his opponents with his flashy skills and explosive speed. </p>


    <p> Every world Cup, there is a new superstar that people will remember.The underdogs, Mexico have not always been able to make it past the round of 16s. World Cup 2014 , the most dramamatic moment that a country has 
    every experienced. Mexico faced Netherlands in the round of 16. In the 48 minute, Mexico took the lead by a stunning strike from Giovanni dos Santos. Mexico were controlling the game until  they started to 
    sleep on Robben and Sneijder. In the 88 minute, Sneijder scored a stunning volley and on the 94 minute, the controversy if it was a penalty or "NO ERA PENAL". 
    </p>
 
 
 <br> <br> 
 <h2> Teams and Players to look out for</h2>
 <br> 
 <table style="width:50%">
  <tr>
    <th>Country</th>
    <th>Players</th>
    
  </tr>
  <tr>
    <td>France</td>
    <td>Kylian Mbappe</td>
  </tr>
  <tr>
    <td>Brazil</td>
    <td>Neymar JR</td>
  </tr>
  <tr>
    <td>Argentina</td>
    <td>Lionel Messi</td>
  </tr>
  <tr>
    <td>Portugal</td>
    <td>Cristiano Ronaldo</td>
  </tr>
  <tr>
    <td>Germany</td>
    <td>Thomas Muller</td>
  </tr>
  <tr>
    <td>England</td>
    <td>Harry Kane</td>
  </tr>
  
</table>


</body>

</html>

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

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

In addition to the other comments, you likely wanted this line:

if request.method == "POST"  and request.form.get('submit'):

to be

if request.method == "POST"  and request.form.get('user'):

to check for the user parameter in the form. This would redirect you to display.html, but following this you would likely want to incorporate the changes others suggested as well.

CodePudding user response:

You're not passing or using the variable correctly. You'll need double braces: {{feedback}}. And while render_template returns a string, you should pass the variable to render_template directly: render_template("form.html", feedback=feedback).

Additionally, the input for the second name isn't closed in form.html (should probably be comments as well, and your image path in teams.html should use the full path from server root (and forward slashes, not backslashes).

  • Related