1

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">&times;</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?

Tomas Jacobsen
  • 2,368
  • 6
  • 37
  • 81

1 Answers1

1

The TEMPLATE_CONTEXT_PROCESSORS setting by default includes the messages context proceessor. So you don't need to do get_messages(request) in your view. In fact, it is harmful in your case: the call to get_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 the messages 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.

sk1p
  • 6,645
  • 30
  • 35
  • I added 'get_messages()' to my view, when the messages did not display. Removing the code in my 'ProfileFrontpage' view and restarting my server, does not do any difference. The message does not display. – Tomas Jacobsen Jan 10 '14 at 13:12
  • Definitely keep the `get_message` removed. How does the rest of the template look like? is the messages output in your base template? If not, is it inside of a block? – sk1p Jan 10 '14 at 13:31
  • I had the message inside a block in a sub template , but moved it out to the base.html template and not inside a block. Still no message. – Tomas Jacobsen Jan 10 '14 at 13:37
  • If you put `{% debug %}` into your template (or inspect the template context with django-debug-toolbar), does it output the `messages` key at all? – sk1p Jan 10 '14 at 17:14