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?
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.)
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With