Azad Rasul
SmartRS

Follow

SmartRS

Follow

8- Create Custom Forms with Flask: A Step-by-Step Guide

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

2 min read

Play this article

This article provides a step-by-step guide on how to create a form using Flask, a Python web framework. You will learn how to create a form that prompts the user to answer the question "What is the most popular programming language?" with three answer fields. Additionally, you will be guided on how to create a results page and a Python script to run the form. By following the instructions provided in this article, you can easily create your own forms using Flask and obtain results based on the user's input.

Create a "form.html" file that contains a form to answer the question "What is the most popular programming language?" with 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>
</form>

Create a "results.html" file to display the results:

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

Prepare a "form.py" script:

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

@app.route('/')
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__':
   app.run(debug = 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:

This article provides instructions on how to create a form using Flask, a Python web framework. Specifically, it shows how to create a form that asks the user to answer the question "What is the most popular programming language?" and provides three fields for the answers. The article also includes instructions on how to create a results page and a Python script to run the form. By following the instructions provided in this article, users can create their own forms using Flask and obtain results based on the user's input.

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

Did you find this article valuable?

Support Azad Rasul by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this