
Welcome!
Everything is fine.
The demo is ln
The Python code is in example_form_processing_one_route.py
:
from flask import (Flask, url_for, render_template, request)
import math
from all_examples import app
@app.route('/ln/', methods=['GET','POST'])
def ln():
if request.method == 'GET':
return render_template('ln.html')
else:
x = request.form.get('x')
try:
y = math.log(float(x))
return render_template(
'ln.html',x=x,y=y)
except:
return render_template(
'ln.html',
msg=('Error computing ln of {x}'
.format(x=x)))
The HTML template is like this: Notice that we don't need to specify
the action
attribute, since it just goes to the same route. However,
there is a possible vulnerability anytime the action
is unspecified,
so best practice is to include it. (I need to update my examples.)
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>Natural Logs</title>
</head>
<body>
<h1>Compute a Natural Logarithm</h1>
{% if msg %}
<p>{{msg}}</p>
{% endif %}
{% if y %}
<p>The Natural Log of {{x}} is {{y}}</p>
{% endif %}
<form method="POST" action="{{url_for('ln')}}">
<p><label>Number to Logify
<input name="x" type="number" step="any">
</label></p>
<p><input type="submit" value="POST"></p>
</form>
<h2>Menu</h2>
<form method="POST" action="{{url_for('ln')}}">
<p><label>Number (radicand)
<select name="x">
<option>Choose One</option>
<option value="2.718281828459">e</option>
<option value="7.389">e squared</option>
</select></label></p>
<p><input type="submit" value="submit"></p>
</form>
</body>
</html>