๐Ÿ“ Django

Messages Framework in Django

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
281
๐ŸŒฟ
Level
Medium

A built-in mechanism for displaying one-time messages to the user.

Sending messages

from django.contrib import messages

def task_create(request):
    if form.is_valid():
        form.save()
        messages.success(request, 'Task created successfully!')
        return redirect('task-list')
    messages.error(request, 'Could not create the task.')

Message levels

messages.debug(request, 'Debug message')
messages.info(request, 'Information')
messages.success(request, 'Success!')
messages.warning(request, 'Warning!')
messages.error(request, 'Error!')

Displaying messages in a template

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

In the base template (base.html)

<!DOCTYPE html>
<html>
<body>
  {% if messages %}
    {% for message in messages %}
      <div class="alert-{{ message.level_tag }}">{{ message }}</div>
    {% endfor %}
  {% endif %}
  {% block content %}{% endblock %}
</body>
</html>

In CBVs

from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic import CreateView

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

Configuration

# settings.py โ€” already configured by default
INSTALLED_APPS = [..., 'django.contrib.messages', ...]
MIDDLEWARE = [..., 'django.contrib.messages.middleware.MessageMiddleware', ...]
TEMPLATES = [{'OPTIONS': {'context_processors': [
    'django.contrib.messages.context_processors.messages',
]}}]

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

๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 279
๐Ÿ“

Swagger Documentation with drf-spectacular

drf-spectacular generates an OpenAPI 3.0 schema and Swagger UI for DRF.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 561
๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315

Did you like the article?

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