Skip to content

Code Artisan Lab

Learn PHP, Python & JavaScript

  • Home
  • Softwares
  • Courses
  • Training
  • My account
  • Contact Us
  • Vendors

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

  • 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
  • Smart Billing Software – Fast, Secure & Scalable Solution for Your Business Smart Billing Software – Fast, Secure & Scalable Solution for Your Business
  • Next-Gen Accounting Software for Modern Businesses Next-Gen Accounting Software for Modern Businesses
  • Modern Accounting Software Modern Accounting Software
  • Blog (12)
  • Flutter (2)
  • JavaScript (3)
  • Mobile App Development (4)
  • Next.js (1)
  • Python Django (9)
  • React Native (1)
  • Web Development (1)

Recent Posts

  • Flutter App with Django Backend Using DRF and PostgreSQL | Complete Guide 2025
  • Flutter App Creation from Scratch | Beginner’s Guide 2025
  • DRF vs FastAPI vs Django Ninja vs Flask: Best Python Web Frameworks Compared in 2025
  • Django vs Laravel: Which Backend Framework is Better in 2025?
  • React Native vs Flutter: Which Framework is Better for Mobile App Development in 2025?
Proudly powered by WordPress
%d