Closing admin django adds a popup to the linked object when saving

In admin django, when a user successfully saves (after my clean method) a new or changed linked object that was edited in a popup, I would like the popup to close and not go to another view.

I believe I can use response_change or response_add to make it switch to another view, but is there a way to close the window?

+3
source share
2 answers

See what the original methods do response_changeor response_add: they return the part of javascript that calls the JS method in the parent window, which closes the popup.

return HttpResponse('''
   <script type="text/javascript">
      opener.dismissAddAnotherPopup(window);
   </script>'''

and in the parent window use a script that has the corresponding method:

function dismissAddAnotherPopup(win) {
    win.close();
}

(The original version passes more parameters, so it updates the parent window with a new object, but you don't need it if you just want to close the window.)

+8
source

There is a potential error with the solution provided by Daniel, see this Django ticket . That is, if you use application/xhtml, then the window will not be closed if you return only the script. This error has since been fixed, although in Django.

<!DOCTYPE html>, Django :

'<!DOCTYPE html><html><head><title></title></head><body>'
'<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script></body></html>' % \
# escape() calls force_unicode.
(escape(pk_value), escapejs(obj)))
+2

Source: https://habr.com/ru/post/1719764/


All Articles