Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to previous page in Django after POST request

I face a problem which I can't find a solution for. I have a button in navbar which is available on all pages and it is a button responsible for creating some content.

View that links with button:

def createadv(request):     uw = getuw(request.user.username)     if request.method =='POST':     form = AdverForm(request.POST, request.FILES)     if form.is_valid():         form.instance.user = request.user         form.save()         return HttpResponseRedirect('/', {'username': request.user.username, 'uw': uw})     args = {}     args.update(csrf(request))     args['username'] = request.user.username     args['form'] = AdverForm()     args['uw'] = uw     return  render_to_response('createadv.html', args) 

If you can see now I always redirect to main page '/' after creating content but I want to go back to the page with which I launched the creation of content.

like image 894
Oleg Avatar asked Mar 04 '16 12:03

Oleg


People also ask

How do I redirect a back page in Django?

M Yes, of course, you just have to pass next={{ request. path|urlencode }} query parameter to the URL of your products page. Then you make your back button with href="{{ request. GET.

How can I get previous URL in Django?

You can do that by using request. META['HTTP_REFERER'] , but it will exist if only your tab previous page was from your website, else there will be no HTTP_REFERER in META dict . So be careful and make sure that you are using . get() notation instead.

What is redirect reverse in Django?

It is called reverse because it is a reverse process of determining which view should be called for a given URL (which process is called resolving). Redirects are not specific to Django or any other web frameworks. Redirect means that for a given URL (or action), the user should be instructed to visit a specific URL.

What is difference between render and redirect in Django?

The render function Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text. You request a page and the render function returns it. The redirect function sends another request to the given url.


1 Answers

You can add a next field to your form, and set it to request.path. After you processed your form you can redirect to the value of this path.

template.html

<form method="POST">     {% csrf_token %}     {{ form }}     <input type="hidden" name="next" value="{{ request.path }}">     <button type="submit">Let's Go</button> </form> 

views.py

next = request.POST.get('next', '/') return HttpResponseRedirect(next) 

This is roughly what django.contrib.auth does for the login form if I remember well.

If you pass through an intermediate page, you can pass the 'next' value via the querystring:

some_page.html

<a href="{% url 'your_form_view' %}?next={{ request.path|urlencode }}">Go to my form!</a> 

template.html

<form method="POST">     {% csrf_token %}     {{ form }}     <input type="hidden" name="next" value="{{ request.GET.next }}">     <button type="submit">Let's Go</button> </form> 
like image 151
Antoine Pinsard Avatar answered Sep 20 '22 00:09

Antoine Pinsard