Built-in form classes
You can create your own form inherited from the Base Form class. And then pass your new class as the value of the “form_class” parameter to the FeedbackView.as_view() call.
ContactFormBase class
- class feedback_form.forms.ContactFormBase
The base contact form class from which all contact form classes should inherit. It will collect name, email address and message. You can to override this class attributes:
- subject_template
A
str
, the name of the template to use when rendering the subject line of the message. By default, this is feedback_form/email_subject.txt.
- message_template
A
str
, the name of the template to use when rendering the body of the message. By default, this is feedback_form/email_template.txt.
And two methods are involved in producing the contents of the message to send:
- get_message()
Returns the body of the message to send. By default, this is accomplished by rendering the template name specified in
message_template
.- Return type
str
- get_subject()
Returns the subject line of the message to send. By default, this is accomplished by rendering the template name specified in
subject_template
.- Return type
str
- **Subject must be a single line**
The subject of an email is sent in a header (named Subject:). Because email uses newlines as a separator between headers, newlines in the subject can cause it to be interpreted as multiple headers; this is the header injection attack. To prevent this,
subject()
will always force the subject to a single line of text, stripping all newline characters. If you overridesubject()
, be sure to either do this manually, or usesuper()
to call the parent implementation.
- prepare_mail()
This method loops through
get_message()
andget_subject()
, collecting those parts into a dictionary with keys corresponding to the arguments to Django’s mail_admins function, then returns the dictionary. Overriding this allows essentially unlimited customization of how the message is generated.- Return type
dict
- get_request_meta()
Returns the selected metadata from the
request_meta
. It is used if CONTACT_SEND_META_INFO is True in ‘settings’ module of the your project.- Return type
dict
Meanwhile, the following attributes/methods generally should not be overridden; doing so may interfere with functionality, may not accomplish what you want, and generally any desired customization can be accomplished in a more straightforward way through overriding one of the attributes/methods listed above.
- request_meta
The dict with some meta data from
HttpRequest
object representing the current request. This is set automatically in __init__(), and is used if CONTACT_SEND_META_INFO is True in ‘settings’ module of the your project.