Django messages not showing after HttpResponseRedirect

后端 未结 2 1841
小蘑菇
小蘑菇 2020-12-31 16:46

Tried tonnes of stuff out there but none of them really helped.

I have a URL for example:

http://localhost:8000/user/edit-transaction/?object_id

2条回答
  •  天涯浪人
    2020-12-31 17:31

    In my case modifying the MESSAGE_STORAGE value as mentioned by @ParagTyagi was not enough. I was having custom change_list.html templates for some admin classes and in those I had to add the following code:

    {% if messages %}
        
      {% for message in messages %} {% endfor %}
    {% endif %}

    Notice that I've commented the

  • tag, otherwise the messages are duplicated.

    In addition, there has to be more than one message in the messages object. Otherwise, the messages are not displayed neither. If you have just one message to display (e.g. a confirmation of a success operation), then just add another empty message like:

    messages.add_message(self.request, messages.WARNING, "")
    

    After the modifications, remember to restart your web server (e.g. Apache) and clear your browser cache.

提交回复
热议问题