Django Contact Form with reCAPTCHA

The quix.django.contact app is a very basic contact form for Django 1.4. It simply allows users to send a message to email addresses specified in settings.py.

This is how the quix.django.contact form can be extended with django-recaptcha to add a reCAPTCHA field to the form.

Both quix.django.contact and django-recaptcha can be installed using pip.

pip install quix.django.contact django-recaptcha

They both need to be in INSTALLED_APPS.

INSTALLED_APPS = (
    # ... 
    'quix.django.contact',
    'captcha',
    # ... 
)

The public and private key obtained from reCAPTCHA are specified in settings.py.

RECAPTCHA_PUBLIC_KEY = 'YOUR reCAPTCHA public key goes here'
RECAPTCHA_PRIVATE_KEY = 'YOUR reCAPTCHA private key goes here'

The email recipient(s) for the contact form are specified in settings.py.

CONTACT_EMAILS = ('somebody@localhost.com',)

By default, the built-in contact form is used. However, a custom form can be specified in settings.py.

CONTACT_FORM_CLASS = 'myproject.forms.ReCaptchaContactForm'

The ReCaptchaContactForm should extend quix.django.contact.forms.ContactForm and add the ReCaptchaField.

/myproject/myproject/forms.py

from quix.django.contact.forms import ContactForm
from captcha.fields import ReCaptchaField

class ReCaptchaContactForm(ContactForm):
    captcha = ReCaptchaField()

You can see this in action on my contact form

This works for other fields too. You could use an "Are you human?" field instead of reCAPTCHA or whatever suits your needs.

Did you enjoy Django Contact Form with reCAPTCHA? If you would like to help support my work, A donation of a buck or two would be very much appreciated.
blog comments powered by Disqus
Linux Servers on the Cloud IN MINUTES