I have a popup window which processes a form. After button click I need to close the popup window and return to the main page. How can I do both actions in my Django view?
def InsertPopup (request):
if request.method == 'POST':
form = CustomerForm(request.POST)
if form.is_valid():
form.save(True)
return HttpResponse('<script type="text/javascript">window.close()</script>')
This code works fine but after the 'window.close' script I want to redirect/get focus back on the main page which shall show the updated data.
Many thanks for help.
In Django, redirection is accomplished using the 'redirect' method. The 'redirect' method takes as argument: The URL you want to be redirected to as string A view's name. In the above example, first we imported redirect from django.
To safely close a pop-up window, locate the button in your Taskbar that corresponds to the pop-up. Normally, the button and the pop-up will have the same title. Right click on the button and select Close.
Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .
In django You can not pass parameters with redirect. Your only bet is to pass them as a part of URL. in your html you can get them from URL. I would use session variables in order to pass some context through a redirect.
This isn't really specific to Django. You could set the location through JavaScript after closing the popup.
window.opener.parent.location.href = "/";
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