0

I want my page to reload and show the django messages sent to the page when a POST call is made (by another user).

From api I am calling the method like that:

 def create(self, request, pk=None):
        json_data = json.loads(request.body)
        sample_path = json_data['sample_path']
        try:
            sample = BloodSample.objects.get(sample_path = sample_path)
            if json_data['status'] == 201:
                BloodSampleAdmin(sample, BloodSample).display_messages(request, sample)
                return Response(json_data['body'], status = status.HTTP_201_CREATED)

and the method in BloodSampleAdmin is:


    def display_messages(self, request, sample):
        messages.success(request, ("hurray"))
        return HttpResponseRedirect(reverse(
            "admin:backend_bloodsample_change",
            args=[sample.id]
        ))

I am 100% sure that the method is called (debug). But message wont pop anyway. I am using Postman to send the POST request.

Any ideas on what is going wrong?

JujV
  • 63
  • 5
  • 1
    Actually your `HttpResponseRedirect` is simply discarded: you call `display_messages`, ignore it's return value and return 201 response instead. If you want page to reload - either `return BloodSampleAdmin(...).display_messages(...)` or add some javascript to do `window.location.reload()` when 201 response was received. – STerliakov Feb 08 '22 at 09:44
  • Augh, you want it to happen with different users? E.g. user A watches page with messages, user B submits new message (and receives 201 response) and then user A can see it real-time? Then you shouldn't do it with plain http request/responce cycle, you need some other stack (I'd prefer WebSocket api using [channels](https://channels.readthedocs.io/en/stable/), there are also [gevent](https://github.com/gevent/gevent) and some other solutions). You can choose long polling instead, but it's unnecessary complicated and computational-heavy. – STerliakov Feb 08 '22 at 09:49
  • Thank you. It works better now and I get the page that I wanted in my reponse (in Postman). But my broweser is still not responding. – JujV Feb 08 '22 at 09:57
  • Yes, with different users. One of the user is the API. Thank you for your idea @SUTerliakov – JujV Feb 08 '22 at 09:58
  • I agree with @SUTerliakov – mon io Feb 08 '22 at 23:08

0 Answers0