python - Django messages not working -
i trying use django's messages framework.
i've done told in https://docs.djangoproject.com/en/dev/ref/contrib/messages/#enabling-messages
i add messages messages.success(self.request, 'updated.')
no messages shown. {% if messages %}
evaluates false.
if print {{ messages }}
<django.contrib.messages.storage.fallback.fallbackstorage object @ 0x7fb701c47b70>
.
what can wrong?
my middlewares
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', )
my template context processors
template_context_processors = [ "django.contrib.auth.context_processors.auth", "django.template.context_processors.debug", "django.template.context_processors.i18n", "django.core.context_processors.request", "django.template.context_processors.media", "django.template.context_processors.static", "django.template.context_processors.tz", "django.contrib.messages.context_processors.messages" ]
my installed apps
installed_apps = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', )
edit
class itemupdateview(loginrequiredmixin, updateview): model = item context_object_name = 'item' form_class = itemform def get_form_kwargs(self): kwargs = super(itemupdateview, self).get_form_kwargs() kwargs["user"] = self.request.user return kwargs def form_valid(self, form): transaction.atomic(), reversion.create_revision(): self.object = form.save() reversion.set_user(self.request.user) reversion.set_comment("updating") messages.success(self.request, "updated") return httpresponseredirect(self.get_success_url()) def get_success_url(self): return self.object.get_update_url()
edit 2
{% if messages %} <ul class="messages"> {% message in messages %} <li{% if message.tags %} class="{{ message.tags }}"{% endif %}> {% if message.level == default_message_levels.error %}important: {% endif %} {{ message }} </li> {% endfor %} </ul> {% else %} no messages {% endif %}
edit 3
this big mystery me.
it doesn't add message if use
def form_valid(self, form): self.object = form.save() messages.success(self.request, "updated") return httpresponseredirect(self.get_success_url())
but add message if use
def dispatch(self, request, *args, **kwargs): messages.success(request, 'updated.') return super(itemupdateview, self).dispatch(request, *args, **kwargs)
you not seeing messages in template because form_valid returns httpresponse, drops request object on floor.
note how solution does pass request object forward.
return super(itemupdateview, self).dispatch(request, *args, **kwargs)
what around make sure messages within view , pass context object. response method won't matter.
from django.contrib import messages class homepage(view): template_name = 'index.html' def get(request): return render(request, self.template_name, { 'messages': messages.get_messages(request) }) def post(request): return httpresponseredirect('/home/')
Comments
Post a Comment