Im trying to get the message framework in django to work.
Here is the interesting bits of my settings.py
INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.messages',
)
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',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)
I have not added anything to TEMPLATE_CONTEXT_PROCESSORS
, so it is the default value.
My view, where I want the message to display:
from django.contrib.messages import get_messages
class ProfileFrontpage(TemplateView):
def get(self, request, *args, **kwargs):
if request.user.is_authenticated():
#Get messages
messages = get_messages(request)
#Get the user
user = self.request.user
#Used benefit
used_benefit_list = Benefit.published.filter(used_benefit__user = user)
return render(request, "accounts/frontpage.html", {"messages": messages, "used_benefit_list": used_benefit_list})
else:
return render(request, 'accounts/not_authenticated.html')
Template:
{% if messages %}
{% for message in messages %}
<div class="alert {{ message.tags }}">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ message }}
</div>
{% endfor %}
{% endif %}
And here is my form that should create a message when the form is valid:
def show_profileform(request):
if request.user.is_authenticated():
user = request.user
ProfileFormInlineFormSet = inlineformset_factory(User, Profile, form=ProfileForm, can_delete=False)
if request.method == "POST":
form = UserForm(request.POST, request.FILES, instance=request.user, prefix="main")
formset = ProfileFormInlineFormSet(request.POST, request.FILES, instance=request.user, prefix="nested")
if 'submit' in request.POST:
if form.is_valid() and formset.is_valid():
u = User.objects.get(username = request.user)
#Save to database
user = form.save()
profile = formset.save()
messages.success(request, "Settings updated", extra_tags='alert-success')
return HttpResponseRedirect('/accounts/')
else:
form = UserForm(instance=request.user, prefix="main")
formset = ProfileFormInlineFormSet(instance=request.user, prefix="nested")
return render(request, "accounts/form.html", {"form":form, "formset":formset})
else:
return render(request, 'accounts/not_authenticated.html')
Anyone know why the message is not visible?
The
TEMPLATE_CONTEXT_PROCESSORS
setting by default includes the messages context proceessor. So you don't need to doget_messages(request)
in your view. In fact, it is harmful in your case: the call toget_messages()
gets the messages and clears the message store. Then, the context processor tries to do the same, but doesn't find any, because your view cleared them. It then proceeds to set themessages
key in your template context and overwrites the value from your view.In short: remove messages handling from your
ProfileFrontpage
view and it should just work.