-
Notifications
You must be signed in to change notification settings - Fork 0
Forms
All form interaction (like all web apps) is through the request/response cycle.
The form can be GET or POST. Normally GET is used for forms that are requesting data to be read and is carried in the URL. POST is used for forms that require the data to be secured or are performing a WRITE operation.
There are two routines for handling the data. One is parse_querystring(environ) the other is parse_querydict(environ)
Parses a query string into a list like [(name, value)]. Then it caches this value in case parse_querystring is called again for the same request.
You can pass the result to dict(), but be aware that keys that appear multiple times will be lost (only the last value will be preserved).
Parses a query string into a python dictionary like {name: value}. If multiple values are given for a key, the values are returned as a list like [value1, value2, ...].
# minimus form GET example
from minimus import Minimus, redirect, parse_querystring
app = Minimus(__name__)
simple_form = """
<h1>My Form</h1>
<form method="get" action="/myform_data">
Name<br />
<input name="name" type="text"><br />
Enter your message<br />
<textarea name="message"></textarea><br />
<input type="submit">
</form>
"""
@app.route('/')
def index(environ):
return """<h1>Index</h1>
<a href="/myform">Send a Message</a><br>
"""
@app.route('/myform')
def myform(environ):
return simple_form
@app.route('/myform_data')
def myform_data(environ):
args = parse_querystring(environ) # all the arguments fetched as list
# or... fetch as a singleton if you prefer
name = parse_querydict(environ).get('name') # name fetched from environ
message = parse_querydict(environ).get('message') # message fetched from environ
return f"<h1>Form Example</h1><p>Thanks for for the message:\n {args}.</p><p>Name: {name}</p><p>Message: {message}</p>"
app.run()The POST example differs a little from the GET example.
In this case, we are going to get all the fields at once into a field dictionary. Then we can use that or we can pick out the data one by one as individual variables.
This routine takes environ as input and returns a MultiDict structure with can be treated as a normal Python dictionary. It will support multiple fields as a list.
from minimus import Minimus, parse_formvars, redirect, parse_querystring
app = Minimus(__name__)
simple_form = """
<h1>My Form</h1>
<form method="post">
Name<br />
<input name="name" type="text"><br />
Enter your message<br />
<textarea name="message"></textarea><br />
<input type="submit">
</form>
"""
@app.route('/')
def index(environ):
return """<h1>Index</h1>
<a href="/form">Send a Message</a><br>
"""
@app.route('/hello')
def hello(environ):
args = parse_querystring(environ)
return f"<h1>Hello</h1><p>Thanks for for the message:\n {args}.</p>"
@app.route('/form', methods=['GET', 'POST'])
def myform(environ):
if environ.get('REQUEST_METHOD') == 'POST':
fields = parse_formvars(environ)
name = fields.get('name')
message = fields.get('message')
print(f"{name} said {message}")
return redirect(f'/hello?name={name}&message={message}')
return simple_form
app.run(host='0.0.0.0', port=5000)