Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a django admin add related object popup window on save

In the django admin, when a user successfully saves (after my clean method) a new or changed related object which was edited in a popup, I'd like the popup to close instead of going to a different view.

I believe I could use response_change or response_add to get it to go to a different view, but is there a way I can get the window to close?

like image 209
Mitch Avatar asked Oct 09 '09 22:10

Mitch


2 Answers

Look at what the original response_change or response_add methods do: they return a piece of javascript that calls a 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, have a script that has the relevant method:

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

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

like image 171
Daniel Roseman Avatar answered Nov 05 '22 15:11

Daniel Roseman


There is a potential bug with the solution Daniel provided, see this Django ticket. That is, if you are using application/xhtml, then the window won't close if you return just the script. This bug has since been fixed though in Django.

The better way would be to add <!DOCTYPE html> tags like the current Django source:

'<!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)))
like image 4
K Z Avatar answered Nov 05 '22 14:11

K Z