How to Create a Contact Form For a Website in Django



Python


In this article, we show how to create a contact form for a website in Django.

With a contact form, a visitor to a website can ask questions, leave comments, suggestions, and feedback.

So the approach that we take when a vistor leaves a comment, the website administrator (which is us) gets emailed the comment.

This is how it normally works for contact forms on websites.

So we will create a contact form on the contact URL and then when a visitor types in a comment, we will email the comment to our website email, where an administrator can see it.

For this project, we created our own separate app, contact. You may choose to do it this way or to just put this code inside of an existing app in your project.

So let's go right into the code.

urls.py File

So the first thing we'll create is the urls.py file.

Within this urls.py file, simply have our website domain name followed by /contact. So if your website is example.com, the URL to the contact page will be, example.com/contact

The code in the urls.py file within the project root directory (the same directory that contains the settings.py file) is shown below.



The code in the urls.py file in the contact app is shown below.



So, again, this code in the urls.py files will have the contact page on the URL, http://www.mywebsite.com/contact


The net thing is we need a form. It's a contact form.

So you can either do this as a Django form or create your own form in custom HTML.

I chose to do it in a Django form, because Django provides built-in validation for the form fields and especially for fields such as email fields, to provide email validation.

So in the forms.py file, I have the following code shown below.



So we've now created a ContactForm in the forms.py file of our app.

The form has 3 fields: name, email, and comment. If you want, you can make certain fields optional, but in this code, I chose to make all 3 required.

Now we can get various information from the user, the user's name, the user's email address, and the user's comment/suggestion/question.


Template File

Next we will show the template file for the contact form page.

It is extremely basic.

We simply render out the Django form.




So, as you can see, it is extremely basic.

We simply have Contact Form in a header tag and then render out the form.

settings.py File

So next, we want to get into the email aspect part of the form.

We've already have created our form.

So next is to go into the email functionality of the form.

How do we configure Django to send emails?

So we set up our email account in the settings.py file.

In order to send out email, you need to use the SMTP (Simple Mail Transfer Protocol) of an email provider.

SMTP allows you to send outgoing mail from an email account. Another protocol, IMAP (Internet Message Access Protocol), allows for you to read incoming mail into an email account.

So the SMTP is really what we want to use.

Gmail is a great email service to use because it's free and they readily provide all of the data you need to use SMTP.

We use gmail for this project to send out emails.

In order for this gmail email account to work, you'll probably have to allow the account to take on less secure apps. This is can be found at the following link on the gmail website: Gmail Less Secure Apps.

So the first thing we have to do is define a few variables in the settings.py file of our Django project.

This is shown in the code below.






So now we have all of our email settings set up within our settings.py file.


views.py File

Now we just have to go into the views.py file to get the data from the form fields within our template file and send an email to ourselves in what the user has entered.

The content of the views.py file is shown below.



So the first thing we have to do is import render, since we are rendering template files.

Next, we must import the ContactForm we created in the forms.py file.

Since we are sending email in this view, we must import the send_mail() function from django.core.mail

We then define our function-based view, contactview

We create 3 variables, name, email, and comment, which will eventually contain the values of the form fields that the user enters in.

We create a variable, form, and set it equal to ContactForm(request.POST or None)

This form variable now contains the ContactForm.

If the form is valid (if all fields have been entered in and the email entered is a valid email), then we store the data from the form fields into the variables, name, email, and comment.

If the user is logged in (authenticated), then we create a subject of which user's comment it is. If the user is not authenticated, then the subject is just, A Visitor's Comment.

The comment is the most complex. We want to extract the user's name and the email from the form and put it in the comment. This is because we want to be able to contact the user back. We then put in the user's comment after a few newline characters.

We then send out the email with the send_mail() function.

The sendmail() function takes in 4 parameters. The first is the subject, in this case, "Comment by a User"

The second is the body of the email.

The third is the email account that is sending out the email. This is the account specified in the settings.py file. The email you put into this account must match the email in the settings.py file.

The fourth is the recipient of the email. In the case of a Contact form, we are the recipient, so I put in the same email address.

Once all of these are in place, we have a Contact form on our contact page.

And this is how we can create a contact form for a website in Django.



Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...