Rohit
Rohit

Reputation: 21

Django Message framework is not showing message in template

Here is my settings file

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
       "django.core.context_processors.request",
       "django.core.context_processors.media",
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = '*******************************'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (

   'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

In template I have added the following code to show up message

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

In my views I am creating message like

print results
if results == ():
    messages.success(request, 'This id is not valid key')
    return HttpResponseRedirect('/subscriber/login/')

I already have imported from django.contrib import messages In the view

I dont know what might I am doing wrong here. I am not able to get any message in the login page.

Please help me out!

Upvotes: 2

Views: 3308

Answers (2)

Krzysztof Bujniewicz
Krzysztof Bujniewicz

Reputation: 2417

Change your context processors to:

TEMPLATE_CONTEXT_PROCESSORS = (
   "django.core.context_processors.request",
   "django.core.context_processors.media",
   "django.contrib.messages.context_processors.messages"
)

This way django will put all messages to context used to render the templates, and therefore you will have access to messages in templates.

BTW. You should never, ever post your SECRET_KEY in public. Never.

Source: https://docs.djangoproject.com/en/dev/ref/contrib/messages/

EDIT: changed settings excerpt OP provided

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
       "django.core.context_processors.request",
       "django.core.context_processors.media",
       "django.contrib.messages.context_processors.messages"
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'h^@hi8e&amp;q4e#h!j4v$x+@y2ngs&amp;3&amp;*o%!u8pi(vp3h8n&amp;0$*a)'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (

   'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

Upvotes: 5

Krotton
Krotton

Reputation: 10059

Try adding django.contrib.messages.context_processors.messages to your template context processors list.

Upvotes: 2

Related Questions