@(python)[ch3]
How recaptcha works!
-
View handler
from flask import Flask, render_template, flash, session, redirect, url_for
from wtforms import TextAreaField
from wtforms.validators import DataRequired
from flask_wtf import FlaskForm
from flask_wtf.recaptcha import RecaptchaField
DEBUG = True
SECRET_KEY = 'secret'
# keys for localhost. Change as appropriate.
RECAPTCHA_PUBLIC_KEY = '6LeYIbsSAAAAACRPIllxA7wvXjIE411PfdB2gt2J'
RECAPTCHA_PRIVATE_KEY = '6LeYIbsSAAAAAJezaIq3Ft_hSTo0YtyeFG-JgRtu'
app = Flask(__name__)
app.config.from_object(__name__)
class CommentForm(FlaskForm):
comment = TextAreaField("Comment", validators=[DataRequired()])
recaptcha = RecaptchaField()
@app.route("/")
def index(form=None):
if form is None:
form = CommentForm()
comments = session.get("comments", [])
return render_template("index_recaptch.html",
comments=comments,
form=form)
@app.route("/add/", methods=("POST",))
def add_comment():
form = CommentForm()
if form.validate_on_submit():
comments = session.pop('comments', [])
comments.append(form.comment.data)
session['comments'] = comments
flash("You have added a new comment")
return redirect(url_for("index"))
return index(form)
if __name__ == "__main__":
app.run()
-
Template
<html>
<body>
{% for comment in comments %}
<p>{{ comment }}</p>
{% endfor %}
<form method="POST" action="{{ url_for('add_comment') }}">
{{ form.csrf_token }}
<p>
{{ form.comment.label }}<br>
{{ form.comment(rows=4, cols=40) }}
</p>
<p>
{% for error in form.recaptcha.errors %}
{{ error }}
{% endfor %}
{{ form.recaptcha }}
</p>
<p>
<input type="submit" value="Add comment">
</p>
</form>
</body>
</html>