xpanta
xpanta

Reputation: 8418

django, return to previous page after form POST submit

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.

Upvotes: 5

Views: 7684

Answers (3)

johncosta
johncosta

Reputation: 3807

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.

Upvotes: 1

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53981

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'])

Upvotes: 13

Meitham
Meitham

Reputation: 9670

current page is a very vague term but i am assuming you want the page that referred you to the form page, this is normally (not always) stored in the HTTP_REFERRER header of the request itself. You could try to fetch that from the request and do a redirect.

Upvotes: 0

Related Questions