working recaptcha v2 integration for contact/ register pages - Cheers!
Hey,
just wanted to let you and others know how easy it is to implement recaptcha v2 for your contact or register pages, right in your existing wtforms. beats writing an email confirmation convoluted rats nest of functions and deters any bots. just login to a gmail acct, go to recaptcha page, enter your site domain and corresponding keys as explained below. Cheers.
# init.py
from flask_wtf import Form, RecaptchaField from wtforms import BooleanField, TextField, TextAreaField, PasswordField, SubmitField, validators
# recaptcha config DEBUG = True SECRET_KEY = 'random alphanumeric key of your choice' RECAPTCHA_PUBLIC_KEY = 'your google recaptcha site key' RECAPTCHA_PRIVATE_KEY = 'your google recaptcha secret key'
app.config.from_object(__name__) app.secret_key = 'random alphanumeric key of your choice - not SECRET_KEY'
class RegisterForm(Form): username = TextField('Username', [validators.Length(min=5, max=20)]) email = TextField('Email Address', [validators.Length(min=8, max=50), validators.Email()]) password = PasswordField('Password', [validators.Length(min=6, max=20, message='Password must be at least 6 characters'), validators.Required(), validators.EqualTo('confirm', message='Passwords must match')]) confirm = PasswordField('') accept_tos = BooleanField('By registering, I accept the <br> <a href="/termsofservice/"> Terms </a> and <a href="/privacy/"> Privacy </a> (date/goes/here)', [validators.Required()]) recaptcha = RecaptchaField()
@app.route('/register/', methods=["GET","POST"]) def register_page(): try: form = RegisterForm()
if request.method == "POST" and form.validate(): username = form.username.data email = form.email.data password = sha256_crypt.encrypt((str(form.password.data))) c, conn = connection()
x = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))
if int(x) > 0: flash("That username is already taken, please choose another.") return render_template("register.html", form=form)