Django – Making a contact form and storing its data without model, query and html

In Django, creating a contact form typically requires managing forms, views, models, and admin registration. However, you can simplify this process using the django-contactforms package, which provides a predefined contact form and model without writing custom code in models.py, views.py, or HTML templates.

Installation and Setup

First, create a Django project and app, then install the required package:

pip install django-contactforms

Add the package to your settings.py:

INSTALLED_APPS += ["contactforms"]

URL Configuration

In your project's urls.py, configure the URL patterns:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('contactforms.urls')),
    path("aftercontact", include('MyForm.urls'))
]

In your app's urls.py, add the redirect endpoint:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name="home"),
]

Views Configuration

Create a simple view in views.py to handle the redirect after form submission:

from django.shortcuts import render

def home(request):
    return render(request, "home.html")

HTML Templates

Create a templates folder in your app directory. Inside it, create a contact folder and add contact.html:

<!DOCTYPE html>
<html>
    <body>
        <div class="row">
            <form action="{% url 'contact' %}" method="post">
                <h3>Send a Message</h3>
                {% csrf_token %}
                <div class="form-group">
                    {% if messages %}
                        {% for message in messages %}
                            <span{% if message.tags %} class="{{ message.tags }}"{% endif %} style="color: green">
                                {{ message }}
                            </span>
                        {% endfor %}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ forms.name }}
                </div>
                <div class="form-group">
                    {{ forms.email }}
                </div>
                <div class="form-group">
                    {{ forms.subject }}
                </div>
                <div class="form-group">
                    {{ forms.message }}
                </div>
                <button class="btn btn-primary" type="submit">Submit</button>
            </form>
        </div>
    </body>
</html>

Create home.html in the main templates folder for the success page:

<!DOCTYPE html>
<html>
    <head>
        <title>Contact Form Success</title>
    </head>
    <body>
        <h3>Message Sent Successfully!</h3>
        <p>Thank you for contacting us. We will get back to you soon.</p>
    </body>
</html>

How It Works

The django-contactforms package provides:

  • Predefined Model: Automatically creates a contact model to store form data
  • Built-in Views: Handles form processing and validation
  • URL Routing: Provides URL patterns for the contact form
  • Admin Integration: Automatically registers the model in Django admin

Accessing Stored Data

Once configured, you can access the submitted contact form data through the Django admin panel. The package automatically creates a model and registers it in the admin interface, allowing you to view and manage all contact submissions without writing additional code.

Conclusion

Using django-contactforms significantly simplifies contact form implementation in Django by providing predefined models, views, and templates. This approach saves development time while maintaining full functionality for collecting and storing user messages.

Updated on: 2026-03-26T00:42:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements