开发者

Django messages framework not working in template loop

I recently upgraded to Django 1.3 and I want to start using the Messages system.

I have added my Middleware, Template context processors and also messages i开发者_如何学Pythonnto the INSTALLED_APPS

MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.csrf.CsrfResponseMiddleware',
    'facebook.djangofb.FacebookMiddleware',
    'annoying.middlewares.RedirectMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
)

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

INSTALLED_APPS = (
    'django.contrib.messages',
)

I'm simply just testing based on a view that makes a simple calculation.

in the admin, the messages show up, however when trying to render them in my base.html file I get the following error.

Caught TypeError while rendering: 'module' object is not iterable

and in the stack it fails here.

{% for message in messages %}

I have also removed the for statement and the I still get the following error, nothing more

<module 'django.contrib.messages' from '/Users/ApPeL/.virtualenvs/mysite.com/lib/python2.7/site-packages/django/contrib/messages/__init__.py'>

Any ideas?


I just encountered this problem. I had included the following in my context processor:

from django.contrib import messages
...
def allrequests(request):
    ctx = {
        ...
        'messages': messages
    }
    return ctx

Make sure you are not setting messages in the context, as it is set in the correct manner by django.contrib.messages.context_processors.messages.


How did you MIDDLEWWARE_CLASSES and TEMPLATE_CONTEXT_PROCESSORS in settings.py, it must look like :

MIDDLEWARE_CLASSES = (
  'django.middleware.common.CommonMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.middleware.csrf.CsrfResponseMiddleware',
  'django.middleware.locale.LocaleMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
)

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

And in INSTALLED_APPS :

'django.contrib.messages'

And in your template (did you forgot the if? ):

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

I hope it will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜