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!