Building Contact Us Form

In this lesson, we are going to create a Contact form. This will allow our visitors to send feedback directly to the admin email address.

The forms we have built in the last few chapters were closely related to the models class. However, It is also possible (and sometimes more feasible) to create forms that stand on its own without having any relationship with the model. To give you an example of the complete process, the Contact form we will build in this chapter will inherit from forms.Form class instead of forms.ModelForm. But before we do that, we have to learn how to send emails with Django.

Sending Email with Django

To send email, Django requires you to add some configurations. The following is a list of some common configuration options provided by Django:

* SERVER_EMAIL: It specifies the email address which Django will use to send error messages to ADMINS and MANAGERS.

* EMAIL_BACKEND: It specifies the name of the backend to use for sending emails. The django.core.mail.backends.smtp.EmailBackend means that Django will use SMTP Server to send emails. Django has many other backends. Here are other two commonly used backends:

  • django.core.mail.backends.filebased.EmailBackend
  • django.core.mail.backends.console.EmailBackend

The former allows us to write the email to a file instead of forwarding it to an SMTP server. And the latter prints the email directly to the console.

* EMAIL_HOST: It specifies the address of the Email Server or SMTP Server.

* EMAIL_HOST_USER: It specifies the username of the SMTP Server.

* EMAIL_HOST_PASSWORD: Password of the SMTP server.

* EMAIL_PORT: Port to use to connect to the SMTP server.

* EMAIL_USE_TLS: It specifies whether to use TLS secure or not.

* DEFAULT_FROM_EMAIL: It specifies the default email address to use for ordinary correspondence from the site managers.

* ADMINS: It specifies a list of people to send error notifications. When the site is in production (i.e DEBUG = False) and any view raises an exception then Django will send an email to all the to all the people specified in the ADMINS list. Each item in ADMINS list is a tuple. For example:

* MANAGERS: It specifies a list of people to send broken link emails for 404 NOT FOUND errors. It accepts emails in the same format as ADMINS.

To enable this feature you’ll have add django.middleware.common.BrokenLinkEmailsMiddleware middleware in the MIDDLEWARE setting in settings.py file.

The following listing shows configurations required to send emails via Gmail SMTP server.

We are in the development phase and just want to send test emails. As a result, we will use the console backend. Open settings.py and add the following options at end of the file:

djangobin/django_project/django_project/settings.py

Testing email using sendtestemail

Django provides sendtestemail command which sends a test email to the specified email id. It uses email id specified in DEFAULT_FROM_EMAIL setting to send the email.

In the terminal enter the following command:

If you get the same output as above, then it means everything is working as expected.

Instead of sending email to the specified email id, you can also have sendtestemail to send emails to ADMINS or MANAGERS by using --admins or --managers options:

send_mail() function

The send_mail() is the easiest way to send emails in Django. It’s syntax is:

Subject: Subject of the email.

message: Message.

from_email: Sender’s email

recipient_list: A list of recipients

On success send_mail() returns 1, otherewise 0.

mail_admins() function

The main_admins() function sends email to the administrators specified in the ADMINS setting. It’s syntax is:

Subject: the subject of the email.

message: message to send.

It uses email specified in the SERVER_EMAIL setting as the sender’s email.

Since we are interested in sending email to the admins, this is the function we are going to use while building the Contact Us page.

Creating Contact Us Page

Open forms.py file and add ContactForm class to wards the end of the file as follows:

djangobin/django_project/djangobin/forms.py

In the views.py file, add a view named contact as follows:

djangobin/django_project/djangobin/views.py

This view shows the Contact form and sends the submitted response to all the admins.

Notice how we are setting the purpose in the in the email body. The data of the purpose field will be the first element of the tuple in purpose_choices, that is b, fb, nf or o. These letters are not very helpful. That’s why we first convert the tuple into a dictionary and then use the key stored in cleaned_data to access the more readable value.

Next, create a template named contact.html in the templates directory with the following code:

djangobin/django_project/djangobin/templates/djangobin/contact.html

Add a new URL pattern in urls.py as follows:

djangobin/django_project/djangobin/urls.py

Next, add a link to Contact us form in the base.html as follows:

djangobin/django_project/djangobin/templates/djangobin/base.html

To view our creation visit http://127.0.0.1:8000/contact/ and try submitting a feedback or two.

After submission you will get a success message like this:

In the shell running the server you should get output as follows:

Leave a Comment