python - Django messages refuse to display -
i've been trying display alert whenever user posts invalid comment on site (such blank post). decided use messages framework, doesn't seem work. i've made sure app, middleware, , template context processors correct, , views using requestcontext object. if use {% debug %}
in template, can see "messages" variable set, nothing in there.
settings.py
installed_apps = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls', ) middleware_classes = ( 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.auth.middleware.sessionauthenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', ) template_context_processors = ("django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.static", "django.core.context_processors.tz", "django.core.context_processors.request", "django.contrib.messages.context_processors.messages", )
views.py
def detail(request, question_id): uid = request.session.get('uid', 0); q = get_object_or_404(question, pk=question_id); context = requestcontext(request, {'question': q, 'uid':uid}); return render(request, 'detail.html', context_instance=context); def post_comment(request, question_id): try: if (request.post['comment_text'] == ""): messages.error(request, "cannot submit empty comment!"); return httpresponseredirect(reverse('detail', kwargs={'question_id': question_id})); else: comment = comment( question = question.objects.get(id=question_id), submitter = request.session['uid'], text = sanitizer.clean(request.post['comment_text']) ); comment.save(); except exception, e: raise e; else: return httpresponseredirect(reverse('detail', kwargs={'question_id': question_id}));
detail.html
{% extends "base.html" %} {% block content %} {% if messages %} <ul class="messages"> {% m in messages %} <li{% if m.tags %} class="{{ m.tags }}"{% endif %}>{{ m }}</li> {% endfor %} </ul> {% endif %} {%endblock%}
by default, messages
use storage.fallback.fallbackstorage
storage backend, "first uses cookiestorage, , falls using sessionstorage messages not fit in single cookie". effectively, uses cookies storage backend. if there site may affecting that, can try setting use sessions specifically:
message_storage = 'django.contrib.messages.storage.session.sessionstorage'
Comments
Post a Comment