📝 Django

SuccessMessageMixin in Django

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
66
🌿
Level
Medium

SuccessMessageMixin is a mixin for displaying a flash message after a form is successfully submitted in a class-based view.

Installation

No installation required — it is built into Django:

from django.contrib.messages.views import SuccessMessageMixin

Basic Usage

from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Task

class TaskCreateView(SuccessMessageMixin, CreateView):
    model = Task
    fields = ['title', 'description', 'priority']
    success_url = reverse_lazy('task-list')
    success_message = 'Task "%(title)s" was created successfully!'

class TaskUpdateView(SuccessMessageMixin, UpdateView):
    model = Task
    fields = ['title', 'description', 'priority', 'status']
    success_url = reverse_lazy('task-list')
    success_message = 'Task updated!'

Dynamic Message

class TaskCreateView(SuccessMessageMixin, CreateView):
    model = Task
    fields = ['title']
    success_url = reverse_lazy('task-list')

    def get_success_message(self, cleaned_data):
        return f'Task "{self.object.title}" was created and assigned to you!'

Displaying in a Template

{% if messages %}
  {% for message in messages %}
    <div class="alert alert-{{ message.tags }}">
      {{ message }}
    </div>
  {% endfor %}
{% endif %}

Inheritance Order

# Correct: SuccessMessageMixin BEFORE the base class
class TaskCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    ...

# Incorrect
class TaskCreateView(CreateView, SuccessMessageMixin):  # will not work
    ...

Equivalent for Function-Based Views

from django.contrib import messages

def task_create(request):
    if form.is_valid():
        task = form.save()
        messages.success(request, f'Task "{task.title}" created!')
        return redirect('task-list')

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 132
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 84
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 73

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!