Django adds contrib.messages from custom middleware

I applied a special middleware that checks certain fields in the user profile. Fields are not required for registration (to make it quick and easy for the user to register), however I would prefer that they fill them out.

My middleware checks to see if they are installed. If not, it notifies the user through a message. I ran into two problems.

  • Whenever I submit a published form because no template displays messages, the middleware will add the message a second time from the moment the middleware was called when the message was sent, and after the redirect, it was called again.

    I solved this problem by repeating the messages in my middleware and checking if there is a message that I am going to add is already there. If so, he does not add it again.

  • When a user fixes a problem by updating their profile, messages are still present when the next page is loaded. After that, everything works. At the beginning of my middleware, I actually put a check that returns None if the request was sent (I would have thought it would solve both problems, but it didn’t).

Any idea how to solve the second problem? Is there a better way to solve my first?

Thank.

Edit:

Is there a way to clear messages in a view? I tried iterating through them (without storage.used = False) and they are still there. I would expect this to solve both of my problems.

+3
source share
4 answers

Yoy can use sticky messages https://github.com/AliLozano/django-messages-extends , which is a single repository that saves only messages in the request, and does not save in the session

+1
source

, django.contrib.messages ? , , . , , - , " ".

: RequestContext ( ), , ( , ).

, , , . , , , , , , .

0

process_reponse() Middleware . , , , .

django.contrib.messages. , " XYZ" , , , , .

0

, . dict.

0

All Articles