0

I'm not sure if what I'm doing is right or wrong. But this functionality brought me across a very esoteric event happening inside Django Views.

So the code:

from django.contrib.messages import get_messages
from django.contrib import messages
#Other required modules.

@require_GET
def get_form(request):
    #code to get a form.
    storage = get_messages(request)
    for message in storage:
        message = message
    if message:
        context['message'] = message
    html = render(request , 'some_html.html' , context)
    return HttpResponse(html)

@require_POST
def submit_form(request):
    #Initialize a bounded form.
    if form.is_valid():
        form.save()
        messages.add_message(request, messages.INFO, success_message)
    else:
        messages.add_message(request, messages.INFO, error_message)    
        # ABOVE LINE IS WHERE THE ISSUE OCCURS
        return redirect('reverse_url_mapping_to_get_form') 
        # Above line is redirecting to "get_form" function(The function preceding this one).

ISSUE : Now the strange thing I encountered occurred when I made a typo in return redirect('reverse_url_mapping_to_get_form')(The last line of the code mentioned.).

Obviously I encountered Django's error page, saying that there is "NoReverseMatch". But when I reloaded the form(normal refreshing through the browser, on the "url" mapping to get_form(request)), I found that the error message which I added in the line messages.add_message(request, messages.INFO, error_message) was being brought to the html(some_html.html). Even thought the error occurred and I never redirected to the url successfully.

Now I understand that messages.add_message() somewhat piggybacks on the request, BUT, shouldn't it have been destroyed in the case when error occurred. The case where the redirect wasn't successfully executed.

Why did the message was still inside the request object when the reload occur?

Shouldn't it have been destroyed(because it was a completely independent GET request; I initiated it from my browser by reloading)?

Thanks in advance.

Shivam Sharma
  • 901
  • 1
  • 6
  • 12

1 Answers1

0

You've completely misunderstood how the messages framework works. It does not store the messages in the request; they are stored in the session.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks a lot for your reply. Just wanted to ask that if a message is not extracted in views for a request hit to the server from `get_messages(request)` by any chance, then is it likely that it'll be extracted the next time when the user's request hit to the server comes across `get_messages(request)` even if the message wasn't meant for that particular request hit to the server? – Shivam Sharma Jul 24 '17 at 16:07
  • I don't really understand what you're asking. Once added, messages persist until they are displayed. This is fully explained in the documentation. – Daniel Roseman Jul 24 '17 at 17:00
  • Apologies for the confusing question. I wanted to know that messages persist or not. Apart from this, I found what I was looking for in the documentation. Thanks a lot again. – Shivam Sharma Jul 24 '17 at 17:16