Reference¶
Views¶
-
class
envelope.views.
ContactView
(**kwargs)¶ Contact form view (class-based).
Displays the contact form upon a GET request. If the current user is authenticated,
sender
andemail
fields are automatically filled with proper values.When the form is submitted and valid, a message is sent and afterwards the user is redirected to a “thank you” page (by default it is the page with the form).
form_class
- Which form class to use for contact message handling.
The default (
envelope.forms.ContactForm
) is often enough, but you can subclass it if you want, or even replace with a totally custom class. The only requirement is that your custom class has asave()
method which should send the message somewhere. Stick to the default, or its subclasses. form_kwargs
- Additional kwargs to be used in the creation of the form. Use
with
envelope.forms.ContactForm
form arguments for dynamic customization of the form. template_name
- Full name of the template which will display the form. By default it is “envelope/contact.html”.
success_url
- URL of the page with some kind of a “thank you for your feedback”, displayed after the form is successfully submitted. If left unset, the view redirects to itself.
-
form_class
¶ alias of
ContactForm
-
form_invalid
(form)¶ When the form has errors, display it again.
-
form_valid
(form)¶ Sends the message and redirects the user to
success_url
.
-
get_initial
()¶ Automatically fills form fields for authenticated users.
-
get_success_url
()¶ Returns the URL where the view will redirect after submission.
-
envelope.views.
filter_spam
(sender, request, form, **kwargs)¶ Handle spam filtering.
This function is called when the
before_send
signal fires, passing the current request and form object to the function. With that information in hand, all available spam filters are called.TODO: more spam filters
Forms¶
-
class
envelope.forms.
ContactForm
(*args, **kwargs)¶ Base contact form class.
The following form attributes can be overridden when creating the form or in a subclass. If you need more flexibility, you can instead override the associated methods such as get_from_email() (see below).
subject_intro
- Prefix used to create the subject line. Default is
settings.ENVELOPE_SUBJECT_INTRO
. from_email
- Used in the email from. Defaults to
settings.ENVELOPE_FROM_EMAIL
. email_recipients
- List of email addresses to send the email to. Defaults to
settings.ENVELOPE_EMAIL_RECIPIENTS
. template_name
- Template used to render the (plaintext) email message. Defaults to
envelope/email_body.txt
. html_template_name
- Template used to render the HTML email message. Defaults to
envelope/email_body.html
.
-
get_context
()¶ Returns context dictionary for the email body template.
By default, the template has access to all form fields’ values stored in
self.cleaned_data
. Override this method to set additional template variables.
-
get_email_recipients
()¶ Returns a list of recipients for the message.
Override to customize how the email recipients are determined.
-
get_from_email
()¶ Returns the from email address.
Override to customize how the from email address is determined.
-
get_subject
()¶ Returns a string to be used as the email subject.
Override this method to customize the display of the subject.
-
get_template_names
()¶ Returns a template_name (or list of template_names) to be used for the email message.
Override to use your own method choosing a template name.
-
save
()¶ Sends the message.
Template tags¶
Add {% load envelope_tags %}
to your template before using any of these.
Returns the HTML for any spam filters available.
Renders the contact form which must be in the template context.
The most common use case for this template tag is to call it in the template rendered by
ContactView
. The template tag will then render a sub-templateenvelope/contact_form.html
.
Spam filters¶
-
envelope.spam_filters.
check_honeypot
(request, form)¶ Make sure that the hidden form field is empty, using django-honeypot.
Signals¶
before_send
Sent after the form is submitted and valid, but before sending the message.
Arguments:
sender
- View class.
request
- The current request object.
form
- The form object (already valid, so
cleaned_data
is available).
after_send
This signal is sent after sending the message.
Arguments:
sender
- Form class.
message
- An instance of
EmailMessage
that was used to send the message.form
- The form object.