Monday, November 27, 2023

htmx progress indicator problem - might be caching

I'm trying but failing to get htmx to deliver a progress indication.

The task is creating records in the database for each item in a list provided by the logged in user.

The view with the submit button collects the list and does the database insertion. I added a "progress" property to the User model as follows ...

class User(AbstractUser):
      def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.progress = ""
    ...
      def set_progress(self, value):
        self.progress = value
      @property
    def get_progress(self):
        return f"{self.progress}"
    ...
    

In the view I update user.progress with the record insertion counter and get_progress returns the correct value in the view itself as proven by print statements. So that part is working.

In the template, I have ...

  <div class="submit-row">
    <input type="submit" value="     {{ btn_label }}      "/>
  </div>
  <p><div hx-get="/hx_usr_progress" hx-trigger="every 1s"></div></p>

... which in theory should fetch progress every second if the htmx docs are correct. See https://htmx.org/docs/#polling

This is the htmx view ...

def hx_usr_progress(request):
    progress = request.user.progress
    print(f"\n{progress} ... {request.user}")
    return HttpResponse(mark_safe(f"<p>... {progress}</p>"))

When the import [Submit] button is clicked, the print statement in hx_usr_progress fires with a blank progress value as per the User.__init__() method AND on the page beneath the Submit button the 3 dots shown above in the last line of the hx_usr_progress() view but nothing further even though the main view is definitely incrementing the count.

That tells me it is being called at least once instead of every second. It should have fired at least 3 or 4 times.

Or if it did perhaps the original response is cached - I don't know.

How can I get this ticking over?

Thanks for any hints.

Cheers

Mike

No comments:

Post a Comment