Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close a popup and redirect to another page in a Django View?

Tags:

django

view

popup

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.

like image 648
Martin Avatar asked Feb 08 '13 22:02

Martin


People also ask

How do I move one page to another in Django?

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.

How do I close a popup?

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.

How do I return a redirect in Django?

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/ .

Can I pass context in redirect Django?

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.


1 Answers

This isn't really specific to Django. You could set the location through JavaScript after closing the popup.

window.opener.parent.location.href = "/";
like image 135
Dirk Eschler Avatar answered Sep 23 '22 06:09

Dirk Eschler