Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django, return to previous page after form POST submit

Tags:

ajax

django

In my web page I have a form that is being filled with data after some ajax requests. For example when a user chooses an item from the list, a simple ajax request is sent to the database that the item has been selected (but not confirmed, yet). Then the list on the web page reloads using a simpe ajax request (just the list, not the whole page) to fetch the new item list.

I think this is more or less a classic cart implementation.

However, when the user presses submit (classic form POST submit, not ajax POST for some reasons concerning the implementation) to confirm the whole list, I would like to return to the current page. (Current page varies) Is this possible? I am using django.

Thanks.

like image 344
xpanta Avatar asked Feb 15 '12 09:02

xpanta


2 Answers

You can supply a next GET parameter when submitting the form, similar to django.contrib.auth's login() method:

https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.views.login:

<form action="/url/to/post/form/?next={{ some_variable }}">

where the variable can simply be the current URL (taken from the request) or a generated URL. In the view processing the form, simply check for a next parameter and redirect appropriately:

from django.shortcuts import redirect
if 'next' in request.GET:
    return redirect(request.GET['next'])
like image 84
Timmy O'Mahony Avatar answered Nov 10 '22 13:11

Timmy O'Mahony


You may be able to use the Post/Redirect/Get Design pattern (PRG). For more general information about Post/Redirect/Get please see the following: http://en.wikipedia.org/wiki/Post/Redirect/Get There are some nice process flow diagrams there.

A generic example of a view implementing PRG might look like the following:

# urls.py
urlpatterns = patterns('',
    url(r'^/$', views.my_view, name='named_url'),
)

# forms.py
class MyForm(forms.Form):
    pass # the form

# views.py
def my_view(request, template_name='template.html'):
    """ Example PostRedirectGet 
    This example uses a request context, but isn't 
    necessary for the PRG
    """
    if request.POST:
        form = MyForm(request.POST)
        if form.is_valid():
            try:
                form.save()
                # on success, the request is redirected as a GET
                return HttpResponseRedirect(reverse('named_url'))
            except:
                pass # handling can go here
    else:
        form = MyForm()

    return render_to_response(template_name, {
        'form':form
    }, context_instance=RequestContext(request))

If you need to do something more interesting with the GET, reverse can take args and kwargs. Manipulate the view params, url_pattern, and reverse call to display the results you would like to see.

One additional note is that you don't have to redirect to the same view (as this example does). It could be any named view that you would like to redirect the user to.

like image 43
johncosta Avatar answered Nov 10 '22 13:11

johncosta