Skip to content

Forms Example 2

jefmud edited this page May 2, 2023 · 6 revisions

Form Example

Here's some sample code for using Python Minimus to create a web application that includes a form route and processing. You will notice this framework uses a pattern that is similar to Bottle and Flask which inspired me to write this framework.

Each route MUST include an "environment" parameter env which carries the entirety of the WSGI request, parsed into key/value pairs of a dictionary.

# formroutes.py
# An example application of form processing

# The basic imports
from minimus import Minimus, render_template, redirect, url_for, parse_formvars
app = Minimus(__name__)

@app.route("/")
def index(env):
    # note the env parameter carries the environment's request dictionary
    return render_template("index.html")

@app.route("/form", methods=["GET", "POST"])
def form(env):
    # note the env parameter carries the environment's request dictionary
    if env.get('REQUEST_METHOD') == "POST":
        # get the named fields from our form
        fields = parse_formvars(env)
        name = fields.get('name')
        email = fields.get('email')
        message = fields.get('message')
        # Do something with the submitted data (e.g. save it to a database, send an email, etc.)
        # ...
        # redirect the the thankyou.html
        return redirect(url_for("thankyou"))
    else:
        return render_template("form.html")

@app.route("/thankyou")
def thankyou(env):
    return render_template("thankyou.html")

if __name__ == "__main__":
    # If we execute the program,
    # Some remote environments will require the parameter host='0.0.0.0'
    app.run(host='0.0.0.0')

Explanation

Here's how this works:

The Minimus class creates a new instance of the web application object. In this case, name refers to the current module (formroutes contained by formroutes.py), so we assign formroutes as the application name. This means that any URL routing requests will go through this instance of Minimus. We define two different web routes. The first one, at /, renders the index.html template whenever someone visits the root page of our website. The second one, at /form, is where our form lives. If someone submits the form by clicking on the submit button, the GET or POST method will call the form() function to handle the submission. Note that we use the fields = parse_formvars(env) function to access the form data, which should contain a variable called fields which will contain key-value pairs for each input element in the form. For example, name = fields.get('name') returns the value entered into the name input field.

After processing the submitted data, we redirect the user to /thankyou. The redirect function takes a string argument representing the endpoint to navigate to, and adds appropriate HTTP headers to ensure that the browser loads the correct HTML file. Finally, we run the web application using the app.run() function.

For completeness, here is the HTML files that are used by the program. These are stored in the templates sub-directory in the files form.html, index.html, and thankyou.html.

form.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Add your head content here -->
  </head>
  <body>
    <!-- The form goes here -->
    <h1>Contact Us</h1>
    <p>Send us a message!</p>
    <form action="/form" method="post">
      Name:<br><input type="text" id="name" required><br>

      Email:<br><input type="email" id="email" required><br>

      Message:<br><textarea id="message" rows="4" cols="50"></textarea><br>


      <button type="submit">Submit</button>
    </form>
  </body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Add your head content here -->
  </head>
  <body>	
    <h1>Minimus Form Example</h1>
    <p>A form example for Minimus</p>
    <p><a href="/form">Click here for the form</a></p>
  </body>	
</html>

thankyou.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Add your head content here -->
  </head>
  <body>
    <h1>Thanks!</h1>
    <p>Thank you for sending data.</p>
    <p><a href="/form">Click here for the form</a></p>
  </body>	
</html>

Clone this wiki locally