0

I am using messages in Django.

I've done exactly as told in the documentation and the messages are showing as they should when my site is in production. But in development at cloud9 they are not showing. What could be wrong?

In development it doesn't add the message if I use

def form_valid(self, form):
    self.object = form.save()
    messages.success(self.request, "Updated")
    return HttpResponseRedirect(self.get_success_url())

but it does add the message if I use

def dispatch(self, request, *args, **kwargs):
    messages.success(request, 'Updated')
    return super(ItemUpdateView, self).dispatch(request, *args, **kwargs)

I don't understand what could be wrong.

2 Answers 2

2

Please make sure you have added middleware in your settings.py in production stage 'django.contrib.messages.middleware.MessageMiddleware' and make sure you have imported messages in your views.py. Also check for 'django.contrib.messages' in your installed apps

Sign up to request clarification or add additional context in comments.

Comments

0

If you want self.request you have to override __init__ method

def __init__(self, *args, **kwargs):
    self.request = kwargs.pop('request', None)
    super(YourForm, self).__init__(*args, **kwargs)

So after that your self.request will work..

2 Comments

I am adding the messages in my UpdateView and not in the form. It is working as it should in production - how does it make sense if I cannot use self.request without the code you suggested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.