DEV Community

ahmed elboshi
ahmed elboshi

Posted on

Django Messages Framework: A Guide to Displaying Messages

Django provides a convenient way to display messages to users using its messages framework. Messages are typically used to provide feedback to users after a certain action has been performed, such as successfully submitting a form or encountering an error.

Setting Up

Firstly, make sure you have a Django project and application set up. If not, you can create one using:

django-admin startproject myproject
cd myproject
python manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode

Next, add 'django.contrib.messages' to the INSTALLED_APPS list in your project's settings:

# myproject/settings.py

INSTALLED_APPS = [
    # other apps
    'django.contrib.messages',
]

Enter fullscreen mode Exit fullscreen mode

Also, ensure you have the MessageMiddleware included in your

MIDDLEWARE:

# myproject/settings.py

MIDDLEWARE = [
    # other middleware
    'django.contrib.messages.middleware.MessageMiddleware',
]

Enter fullscreen mode Exit fullscreen mode

Don't forget to include the {% messages %} template tag in your base template where you want messages to be displayed:

<!-- myapp/templates/base.html -->

{% block content %}
  {% block messages %}
    {% messages %}
  {% endblock messages %}
  <!-- other content -->
{% endblock content %}

Enter fullscreen mode Exit fullscreen mode

Displaying Messages

Now, let's see how to display messages in your views. In your views, you can use the messages module:

# myapp/views.py
from django.contrib import messages
from django.shortcuts import render, redirect

def my_view(request):
    messages.success(request, 'This is a success message.')
    messages.error(request, 'An error occurred.')
    messages.warning(request, 'This is a warning.')
    messages.info(request, 'Just an informational message.')

    return render(request, 'myapp/my_template.html')

Enter fullscreen mode Exit fullscreen mode

These messages are stored in the request object and will be available until the end of the current request-response cycle.

Displaying Messages in Templates

To display these messages in your template, you can use the {% messages %} template tag. For example:

<!-- myapp/templates/myapp/my_template.html -->

{% block content %}
  {% block messages %}
    {% messages %}
  {% endblock messages %}

  <!-- other content -->
{% endblock content %}

Enter fullscreen mode Exit fullscreen mode

This tag will render the messages in a styled format, with different styles for each message level (success, error, warning, info).

Customizing Message Levels

Django messages support several levels: DEBUG, INFO, SUCCESS, WARNING, and ERROR. You can customize the appearance of messages by using specific tags in your template

<!-- myapp/templates/myapp/my_template.html -->

{% block content %}
  {% block messages %}
    {% messages %}
  {% endblock messages %}

  {% messages_debug %}
  {% messages_info %}
  {% messages_success %}
  {% messages_warning %}
  {% messages_error %}

  <!-- other content -->
{% endblock content %}

Enter fullscreen mode Exit fullscreen mode

These tags will render messages of the corresponding levels.

Conclusion

The Django messages framework is a powerful tool for providing feedback to users. Whether you want to display success messages, errors, or warnings, Django makes it easy to implement in a clean and organized way. Happy coding!

Top comments (0)