Skip to content

Introduction to Message Framework

Introduction

The message framework is a feature that allows us to store and display messages to the user. It helps to provide information to the user after certain actions have been performed. We often use message framework to display success messages, error messages, or any other kind of feedback to the user.

Lets take an example: Suppose, you want to show a success message after a user submits a form or an error message when there's a data validation error. In this case, you can use message framework to display one time message to that user. This type of approach helps to improve the user experience and keeps the user informed about what's happening in the application.

How to enable message framework ?

Now, lets discuss how to enable message framework in django ?
When you create a new django project, by default, message framework is already installed.
You need to make sure that the 'django.contrib.messages' app is added to the INSTALLED_APPS list in your Django project's settings file and, you need to make sure that the middleware for message framework i.e. 'django.contrib.messages.middleware.MessageMiddleware' middleware is included in the MIDDLEWARE list.

Types of message engines

Now, lets dicuss about where the messages are stored.
Actually, Django supports different types of message storage engines, which determine where the messages are stored. The default message engine is the cookie-based engine, which stores messages in cookies.
Django also supports other engines like session-based storage, which stores messages in the user's session, and cache-based storage, which stores messages in the cache backend. You can choose the appropriate engine based on your application's needs.

Message Levels and their uses

Now, lets discuss about the different message levels provided by django.

In Django, messages can have different levels to indicate the level of their severity. The available message levels are: debug, info, success, warning and error.

  • DEBUG is used for development purposes and providing detailed information.
  • INFO is used for general information messages.
  • SUCCESS is used to indicate successful operations or actions.
  • WARNING is used to display warnings or potential issues.
  • ERROR is used to indicate errors or failures.

So, that was all about the basic concepts of what a message framework is and we will learn more in depth in the next upcoming videos.