2

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!

2 Answers 2

5

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',
)
Sign up to request clarification or add additional context in comments.

2 Comments

Nice and thanks but I already added middleWare why that is not working
that is not middleware, that is context processor. Middleware is used to modify incoming request or rendered response, while context processor is used to modify data sent from your view to your template.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.