Description

The recontact package is a pluggable django application which provides a contact form that generates an email message to specified addresses. The form is protected by a Google invisible recaptcha.

Installation

Install with pip:

$ python3 -m pip install django-recontact

Example

A minimal Django project is included, with an example contact form.

To set up the example server, run the following commands:

$ python3 -m venv pythonenv
$ source pythonenv/bin/activate
$ python -m pip install django
$ python setup.py install
$ cd example
$ python manage.py migrate
$ python manage.py runserver

You should now have an http server running on localhost port 8000. Connect to localhost:8000 with your web browser to test the form.

Usage

To minimally deploy this app in your Django project you need to do the following:

  • Add ‘recontact’ to INSTALLED_APPS in your project settings.py file.

  • Add the following to the urlpatterns list in your project urls.py:

    url(r'^contact/', include('recontact.urls')),
    

    (Modify the regular expression r’^contact/’ if you want a different url.)

  • Configure the app as described in the next section.

Configuration

To configure the app you must define a dict named RECONTACT_CONFIG in your project’s settings.py. This dict, which is used in the example, shows the default values of all of the parameters:

# Default configuration for the recontact app
RECONTACT_CONFIG = {
    'template_name': 'recontact/contact.html',
    'confirmation_template_name': 'recontact/message_sent.html',
    'addresses': ['postmaster@localhost'],
    'extra_context': {},
    'allow_click_bait': False,
    'query': 'Recontact query:',
    'site_key': '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
}

The site_key shown above is Google’s testing key. It does no checking and displays a badge with a red warning printed across it. You will need to register your site with recaptcha and use a real site_key provided by Google.

The value of ‘addresses’ is either a list of the email addresses to which the contact email generated by the app should be sent, or a callable which returns such a list. In the latter case, the callable will be passed the same arguments as the post method of the RecontactView. In particular, if you have named arguments in the url regex then these will be passed.

The value of extra_context may be a dict containing context to be used when rendering the contact page and acknowledgment page, or a callable which returns such a dict.

The two templates which are provided with the app are minimal and have no styling. You will want to modify these to your taste. The first template manages a form which sends the contact email. The second simply informs the user that a message has been sent. You should use the first two dict entries to specify the locations of your templates.

Unfortunately, the google recaptcha is not that hard to defeat, and there are many bots available which can send messages through a contact page protected by a recaptcha. There may even be humans who find it worthwhile to paste messages into contact pages. The vast majority of the spam messages received by a contact page contain a short come-on message and a link. Presumably the link belongs to a pay-per-click advertiser from whom the spammer is hoping to collect payments. For this reason, django-recaptcha, by default, will not validate fields containing urls. This makes the contact page useless to a click-bait spammer, while still allowing someone who is actually interested in contacting you to do so. However, if you really want to receive messages containing URLs then you can set the allow_click_bait option to True in the configration.

The ‘query’ value is used to generate the subject line of the contact email, which also contains the name provided on the contact form. You should assign a value which allows you to recognize which Django project generated the message.

Forms

class recontact.forms.RecontactForm(data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=<class 'django.forms.utils.ErrorList'>, label_suffix=None, empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None)[source]

Django form which collects data for a contact email message.

Views

class recontact.views.RecontactView(**kwargs)[source]

A class based view which sends a contact email message and then redirects to an acknowledgement page.