'Django - Update button checkbox to all users in realtime

I have a checkbox like this in django app, this is updated checking actual value in database when page is loaded, when user change that is updated again. This is a global switch where all users can see, my question is: how can i update this to all users if one change that?

<input type="checkbox" class="form-check-input" id="customSwitch1" checked>

I test update that every 5 seconds using sockets and this work. But i don't know if exist other method to do that directly with django.

Any suggestion?



Solution 1:[1]

You can use ajax in your template and do a get request with an interval. if your are not doing an external api call it is an option.

   $.ajax({

        type: 'GET',
        url : "{% url "youre urls.py url in django%}",
        success: function(response){
            console.log(response);
         
            for (var key in response.messages)
//do your thing

            
        },
        error: function(response){
            alert('An error occured')
        }
    });
},1000);   --> your interval

          

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 saro