warning admin users about unsaved admin form

Hi

Is there a way of warning admin users about the unsaved changes in a Django admin form when closing the page?

I am using Django 4.0.5

Thank you

That requires some JavaScript. You’ll need to create an event listener for the beforeunload event. You’ll also need to listen to the change event on any of the input fields, or track the original value of those fields to determine whether a change has been made that would require such a warning.

(The “simple” method simply tracks whether any field has changed. The “complex” method requires you to check every field to see if any of them has a different value than when the page was rendered.)