Skip to content

Code Artisan Lab

Learn PHP, Python & JavaScript

  • Home
  • Services
    • Web Designing
    • Web Development
    • Mobile App Development
    • API Development
  • Softwares
  • Mobile App
  • Training
  • My account
  • Contact Us
  • Vendors
  • Enquiry Now

Django Tutorial Part 7: Create a Contact Form with Email Sending (2025 Beginner Guide)

Welcome to Part 7 of our Django beginner series! Today we’ll build a working contact form that sends messages to your email. This is ideal for feedback forms, contact pages, or customer inquiries.

What You’ll Learn

  • Create a contact form using Django forms
  • Send form data via email
  • Configure email backend for development
  • Display success messages

Step 1: Configure Email Settings

In your settings.py, add these for development (console backend):

1
2
3
4
5
6
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.example.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your@email.com'
EMAIL_HOST_PASSWORD = 'your-password'

Note: Use console backend for testing. For real email, use SMTP settings (like Gmail, SendGrid, etc).

Step 2: Create the Contact Form

In forms.py:

1
2
3
4
5
6
7
from django import forms
 
class ContactForm(forms.Form):
    name = forms.CharField(max_length=100)
    email = forms.EmailField()
    subject = forms.CharField(max_length=200)
    message = forms.CharField(widget=forms.Textarea)

Step 3: Create the Contact View

In views.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from django.core.mail import send_mail
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import ContactForm
 
def contact_view(request):
    form = ContactForm()
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            message = f"From: {form.cleaned_data['name']} <{form.cleaned_data['email']}>\n\n{form.cleaned_data['message']}"
            from_email = form.cleaned_data['email']
            send_mail(subject, message, from_email, ['admin@yourdomain.com'])
            messages.success(request, 'Your message has been sent successfully.')
            return redirect('contact')
    return render(request, 'myapp/contact.html', {'form': form})

Step 4: Create the Template

In templates/myapp/contact.html:

1
2
3
4
5
6
7
8
9
10
11
12
13
<h2>Contact Us</h2>
 
{% if messages %}
  {% for message in messages %}
    <p style="color: green;">{{ message }}</p>
  {% endfor %}
{% endif %}
 
<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Send Message</button>
</form>

Step 5: Add URL Pattern

In myapp/urls.py:

1
2
3
4
5
6
from . import views
 
urlpatterns = [
    # ... other urls
    path('contact/', views.contact_view, name='contact'),
]

Step 6: Test the Form

Start the development server:

1
python manage.py runserver

Go to http://127.0.0.1:8000/contact/ and test the form. If using the console backend, the message will appear in your terminal.

Optional: Use HTML Email

If you want to send HTML-formatted messages:

1
2
3
4
5
from django.core.mail import EmailMessage
 
email = EmailMessage(subject, message, from_email, ['admin@yourdomain.com'])
email.content_subtype = "html"
email.send()

Conclusion

You now have a working contact form with email functionality in Django! This feature is essential for communication and adds professionalism to your project.

Coming Up in Part 8: We’ll implement pagination and search to organize large content lists efficiently!

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Like this:

Like Loading...
This entry was posted in Blog, Python Django and tagged django beginner contact form, django contact form, django contact form backend, django contact form example, django contact us page, django email html message, django email integration, django email setup, django email testing, django email tutorial, django email with form, django feedback form, django form email, django mail settings, django send email, django send mail html, django smtp email, django tutorial part 7, django user contact form on June 14, 2025 by surajkumar.

Post navigation

← Django Tutorial Part 6: Upload and Display Images & Files (2025 Beginner Guide) Next.js Tutorial for Beginners: Create a Simple Blog App with Tailwind CSS →

Products

  • Create Invoices Easily with InvoiceUp Create Invoices Easily with InvoiceUp
  • TradeGo - Add Listing Everywhere TradeGo - Add Listing Everywhere
  • Expense Tracker - Easily Track Expense Anytime Expense Tracker - Easily Track Expense Anytime
  • Smart GST Accounts Software Smart GST Accounts Software
  • Complete GST Billing Software Complete GST Billing Software
  • Fast & Easy Restaurant Billing Software Fast & Easy Restaurant Billing Software
  • Easy Appointment Booking Easy Appointment Booking
  • All-in-One Hotel Booking Management Software All-in-One Hotel Booking Management Software
  • Manage Projects Faster with Laravel & MySQL System Manage Projects Faster with Laravel & MySQL System
  • Modern Learning Management System Modern Learning Management System
  • Blog (12)
  • Bootstrap (2)
  • Flutter (2)
  • JavaScript (6)
  • JWT (2)
  • Mobile App (4)
  • Mobile App Development (9)
  • Next.js (7)
  • Nuxt.js (1)
  • Offline Training (3)
  • Python Django (15)
  • React .js (5)
  • React Native (5)
  • Services (13)
  • Tailwind (2)
  • Web Development (13)

Recent Posts

  • Learn Tailwind CSS in Ten Days with Easy Step-by-Step Guide
  • Learn Bootstrap in Ten Days with Easy Step-by-Step Guide Plan
  • Bootstrap and Tailwind CSS Comparison for Modern, Responsive Web Development
  • Django vs Python: Understanding Which Is Easier for Developers Today
  • Why JWT Is Better Than Other Authentication Methods for Security?
Proudly powered by WordPress
%d