pip install WTForms-JSON
python
from flask import Flask, request, render_template
from wtforms import Form, StringField
from wtforms.validators import DataRequired
from wtforms_json import init
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
init()
python
class MyForm(Form):
python
@app.route('/form', methods=['GET', 'POST'])
def form_handler():
if request.method == 'POST' and form.validate():
name = form.name.data
age = form.age.data
return render_template('success.html', name=name, age=age)
return render_template('form.html', form=form)
html
<!-- form.html -->
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="POST" action="/form">
{{ form.csrf_token }}
{{ form.name.label }} {{ form.name }}
<br><br>
{{ form.age.label }} {{ form.age }}
<br><br>
</form>
</body>
</html>
html
<!-- success.html -->
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>