Skip to content

Instantly share code, notes, and snippets.

@Logic-gate
Last active November 22, 2015 17:50
Show Gist options
  • Save Logic-gate/ea11b6d3bca62c460b93 to your computer and use it in GitHub Desktop.
Save Logic-gate/ea11b6d3bca62c460b93 to your computer and use it in GitHub Desktop.
Flask-RECAPTCHA
#Simple example for using wtf-Flask's RECAPTCHA widget.
#APP.py
from flask import Flask, request, render_template
from flask.ext.wtf import Form
from flask.ext.wtf.recaptcha import RecaptchaField
from wtforms.fields import StringField
from wtforms.validators import DataRequired
RECAPTCHA_OPTIONS = {'theme': 'red'} # You can change the theme -- > https://developers.google.com/recaptcha/docs/customization
RECAPTCHA_PUBLIC_KEY = "YOUR PUBLIC KEY" #https://www.google.com/recaptcha
RECAPTCHA_PRIVATE_KEY = "YOUR PRIVATE KEY" #https://www.google.com/recaptcha
app = Flask(__name__)
app.config.from_object(__name__)
class EmailForm(Form):
email = StringField("Email", validators=[DataRequired()])
recap = RecaptchaField()
@app.route('/send', methods = ['GET', 'POST'])
def sendEmail():
send = EmailForm()
if request.method == 'POST':
if send.validate_on_submit():
email = request.form.get('email', None) #get the value of input email. This does not include email validation. This example is for RECAPTCHA
if email is None:
return render_template("send.html", content='Error...Email field is empty', form=send)
else:
return render_template("send.html", content='Form Posted...RECAPTCHA Valid')
return render_template("send.html", content='Please enter a correct RECAPTCHA', form=send) #return the same page
elif request.method == 'GET':
return render_template("send.html", form=send, content='Please Enter Your Email')
#send.html
{% extends "layout.html" %}
{{ content }
{{ form.email(type="text")}}
{{ form.recap() }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment