Django Messages Framework

The previous post made use of the Django messages framework to display a one-time message after an HTTP redirect.

Using the messages framework is simple. First, as explained in the docs, adjust your settings file:

  • ‘django.contrib.messages’ is in INSTALLED_APPS
  • MIDDLEWARE_CLASSES contains ‘django.contrib.sessions.middleware.SessionMiddleware’ and ‘django.contrib.messages.middleware.MessageMiddleware’
  • TEMPLATE_CONTEXT_PROCESSORS contains ‘django.contrib.messages.context_processors.messages’

You can now use the framework in a view:

And display it within a template:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s