Azad Rasul




8- Forms in Flask

Azad Rasul's photo
Azad Rasul
·Jan 5, 2023·

1 min read

Play this article

Create “form.html” file that contains a form to answer a question: “What is the most popular programming language?” and three fields for answers:

<form action = "http://localhost:5000/result" method = "POST">
    <p>What is the most popular programming languages?</p>
    <p>First <input type = "text" name = "First popular" /></p>
    <p>Second <input type = "text" name = "Second popular" /></p>
    <p>Third <input type = "text" name = "Third popular" /></p>
    <p><input type = "submit" value = "submit" /></p>

And create a “results.html” file to return results:

      <table border = 1>
         {% for key, value in result.items() %}
               <th> {{ key }} </th>
               <td> {{ value }} </td>
         {% endfor %}

And now prepare a “” script:

from flask import Flask, render_template, request
app = Flask(__name__)

def student():
   return render_template('form.html')

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

if __name__ == '__main__': = True)

Run the Python script and the following page should be opened:

Fill out the form and submit it to get the results page like this:

* If you like the content, please SUBSCRIBE to my channel for the future content

Share this