Building Contact Us Page

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

Open from the blog app and append the Feedback Model class to the end of the file as follows:



class Feedback(models.Model):
    name = models.CharField(max_length=200, help_text="Name of the sender")
    email = models.EmailField(max_length=200)
    subject = models.CharField(max_length=200)
    message = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = "Feedback"

    def __str__(self):
        return + "-" +

To commit the changes run makemigration followed by migrate command.

(env) C:\Users\Q\TGDB\django_project>python makemigrations
Migrations for 'blog':
    - Create model Feedback
(env) C:\Users\Q\TGDB\django_project>
(env) C:\Users\Q\TGDB\django_project>python migrate
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
  Rendering model states... DONE
  Applying blog.0012_feedback... OK

Open blog's file and add the following URL pattern towards the beginning of the urlpatterns list:


urlpatterns = [
    url(r'^feedback/$',, name='feedback'),
    url(r'^blog/$', views.test_redirect, name='test_redirect'),

Now create a FeedbackForm class in the blog's as follows:


from .models import Author, Tag, Category, Post, Feedback
from django.template.defaultfilters import slugify

class PostForm(forms.ModelForm):

class FeedbackForm(forms.ModelForm):

    class Meta:
        model = Feedback
        fields = '__all__'

Next, create a view function called feedback() at the end of the blog app's file as follows:


from .models import Author, Tag, Category, Post
from django.contrib import messages
from .forms import FeedbackForm

def test_redirect(request):
    c = Category.objects.get(name='python')
    return redirect(c)

def feedback(request):
    if request.method == 'POST':
        f = FeedbackForm(request.POST)
        if f.is_valid():
            messages.add_message(request, messages.INFO, 'Feedback Submitted.')
            return redirect('feedback')
        f = FeedbackForm()
    return render(request, 'blog/feedback.html', {'form': f})

This view shows feedback form and saves the submitted feedback to the database

Next, we need a template to show the Contact Form. Create a new template called feedback.html inside the blog app i.e blog/templates/blog with the following code:


{% extends "base.html" %}

{% block title %}
    Feedback - {{ block.super }}
{% endblock %}

{% block content %}

    {% if messages %}
    <ul class="messages">
        {% for message in messages %}
        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
        {% endfor %}
    {% endif %}

    <div class="content">
    <div class="section-inner clearfix">

        <h3>Submit Your feedback</h3>       

        <form action="" method="post">
            {% csrf_token %}
            {{ form.as_table }}                   
                    <td><input type="submit" value="Submit"></td>


{% endblock %}

To view our creation visit and try submitting a feedback or two.


Adding Feedback Model to Django Admin #

Open blog's file and add FeedbackAdmin class to the file as follows:


class TagAdmin(admin.ModelAdmin):

class FeedbackAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'subject','date',)
    search_fields = ('name', 'email',)
    date_hierarchy = 'date', PostAdmin), CategoryAdmin), AuthorAdmin), TagAdmin), FeedbackAdmin)

Visit Feedback list page ( in the Django Admin and you should get a page like this:


Sending Emails #

Now our site has the ability to accept feedback from the user and site admins can view them using Django Admin. We also want to inform the site admins whenever a new feedback is submitted. To do that, we have to learn how to send email in Django.

Before sending emails in Django, we have to set some variables in the Open and add the following variables towards the end of the file:



# Email settings

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

    ('OverIQ', ''),



Here is a rundown of each variable we have set:

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:

  1. django.core.mail.backends.filebased.EmailBackend
  2. 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. In this case, we are using Gmail SMTP Server i.e

EMAIL_HOST_USER: It specifies the username of the SMTP Server specified in EMAIL_HOST.

EMAIL_HOST_PASSWORD: Password of the SMTP server specified in the EMAIL_HOST.

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. In our case SERVER_EMAIL, EMAIL_HOST_USER and DEFAULT_FROM_EMAIL is same, so Django will use "" for ordinary correspondence as well as for reporting errors.

ADMINS: It specifies a list of people to send error notifications. When the site is in production (i.e DEBUG = False) and views 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:

ADMINS = [    
    ('name1', ''),
    ('name2', ''),

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.

MANAGERS = [    
    ('name1', ''),
    ('name2', ''),

To enable this feature you have add 'django.middleware.common.BrokenLinkEmailsMiddleware' middleware in the MIDDLEWARE setting in To do so, open file and append 'django.middleware.common.BrokenLinkEmailsMiddleware' middleware to the MIDDLEWARE list as follows:





Remember that Django will automatically send emails to people listed in ADMINS and MANAGERS only in production (i.e DEBUG=False). That's why, we won't be able to fully test these functionalities until we deploy our site, which we will do in Deploying Django Project lesson.

Testing email using sendtestemail #

Django provides a sendtestemail command which sends a test email to the specified email id. This command is commonly used to test whether everything is working as expected or not. It uses email id specified in DEFAULT_FROM_EMAIL setting to send the email.

In the command prompt or terminal enter the following command:

(env) C:\Users\Q\TGDB\django_project>python sendtestemail

The above command would send an email to from You should replace with the email where you want to send the test mail.

On my machine, When I tried to run the above command, I got the following error:

(env) C:\Users\Q\TGDB\django_project>python sendtestemail
    self.connection.login(self.username, self.password)
  File "c:\python34\Lib\", line 652, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <
nin/continue?sarp=1&scc=1&plt=AKgnsbsO\n5.7.14 PsvPm3vLbfhrrNnip0yCTlTTsR5im7B5-
NwDUIjbKHmIw4vQQ0LLZgTXixiWw79qfTjMKJ\n5.7.14 hhK67f2kOZUj_ivkwpI-c1GDcPtCqc28fY
EGgAQvI2RzDTzmkCO1hIxnAkeiVI6wPM-5ZK\n5.7.14 KQ1q2GPLhrPL790icanRXSmdIBh0i5nHySo
rk-8Bsv7amSZDgep9kspYGnqRqHesQE2_Gr\n5.7.14 PLwBgB-RmMcfa53-91-aue4ZywDvw> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at
\n5.7.14 b74sm21087287pfl.58 - gsmtp')

Notice the error in the fourth line of the output i.e smtplib.SMTPAuthenticationError. This is a common problem when you try to send email from a Gmail account. This error may occur due to the following reasons:

  1. You have entered wrong username/password in file.
  2. 2-factor authentication is enabled on your account.
  3. "Allow less secure apps" setting is turned off.

First off, make sure you have entered correct username and password in file. Next, check whether you have enabled 2-factor authentication on your Gmail account by visiting If so, disable it. At last, check whether you have "Allow less secure apps" setting turned off by visiting If so, turn it on. In my case, the culprit was "Allow less secure apps".

After doing these steps you should be able to send a test mail using the sendtestemail command.

If sendtestemail succeeds in sending the email, you should get a response like this:


Instead of sending email to the specified id, you can also use sendtestemail to send email to ADMINS or MANAGERS by using the following command:

(env) C:\Users\Q\TGDB\django_project>python sendtestemail --managers

(env) C:\Users\Q\TGDB\django_project>python sendtestemail --admins

As the situation stands, our feedback() view just saves the submitted feedback into the database, it doesn't send any email to the site ADMINS or MANAGERS. In the next section, we are going to change this.

The mail_admins() function #

The main_admins() function sends the email to the site admins i.e list of emails specified in the ADMINS list in It's syntax is:


mail_admins(subject, message, fail_silently=False)

Subject: The subject of the email.

message: The message.

fail_silently: On error mail_admins() raises gaierror (gai stands for getaddrinfo()) which translates to 500 Internal Server Error on production. If for some reason you want to ignore errors silently set this parameter to True.

The mail_admins() uses SERVER_EMAIL setting as sender's email.

Let's modify feedback() view function in blog's to use mail_admins() function as follows:


from .forms import FeedbackForm
from django.core.mail import mail_admins

def test_redirect(request):

def feedback(request):
    if request.method == 'POST':
        f = FeedbackForm(request.POST)

        if f.is_valid():
            name = f.cleaned_data['name']
            sender = f.cleaned_data['email']
            subject = "You have a new Feedback from {}:{}".format(name, sender)
            message = "Subject: {}\n\nMessage: {}".format(f.cleaned_data['subject'], f.cleaned_data['message'])
            mail_admins(subject, message)

            messages.add_message(request, messages.INFO, 'Feedback Submitted.')
            return redirect('feedback')

        f = FeedbackForm()
    return render(request, 'blog/feedback.html', {'form': f})

Visit feedback page at, fill out the form and hit Submit.


In addition to saving the feedback to the database this time feedback() view will send an email to the site admins informing them about the submitted feedback. This is how email will like:


On the other hand, if main_admins() encounters an error while sending emails then it will throw gaierror exception like this:


In the production, users will get a generic 500 Internal Server Error page.

Note: To checkout this version of the repository type git checkout 22a.