0

I am trying to redirect the user to the previous page once a form is submitted.

  1. User starts on Venue_id page (http..../show_venue/23/) and clicks on product id
  2. User is directed to Product_id page (http..../show_product.../1/) and find a form
  3. User completes form and submit on Product_id page
  4. user redirect to Venue_id page upon form submission (from http..../show_venue/23/)

http..../show_venue/23/ -> http..../show_product.../1/ -> http..../show_venue/23/

I found a good source of inspiration on the forum, particularly on this page (How to redirect to previous page in Django after POST request)

(I also found some posts using the history of the browser. But decided to stick to this method, as it seems using browser history doesn't always work)

I used next as suggested in the code in the above post. But there is something I don't understand which is probably why I get this wrong.

Here is the different codes in views I tried for next:

  1. next = request.POST.get('next','/') => this sends me to '/', which is not what i want. However it seemed to work for the person who posted the original question even though they were trying to NOT be redirect to '/';
  2. next = request.POST.get('next','') => sends me to my product_id page url, but the page is empty
  3. next = request.POST.get('next') => this one was suggested in other posts, but I get Field 'id' expected a number but got 'None'.

I might be completely wrong, but I feel the key is probably there. How to do refer to "show_venue/<venue_id>" into "next = request.POST.get('next','show_venue/<venue_id>')"?

In terms of code

Views

def show_product_from_venue(request, product_id):
    product = Product.objects.get(pk=product_id)
    form = ReviewForm()
    venue_form = VenueForm()
    submitted = False
    next = request.POST.get('next')
    if request.method == "POST" and 'btnvenue_form' in request.POST:
        venue_form = VenueForm(request.POST)
        if venue_form.is_valid():
            venue_form.save()
            return HttpResponseRedirect(next)
        else:
            venue_form = VenueForm
            if 'submitted' in request.GET:
                submitted = True
    else:
        print(form.errors)
    return render(request,"main/show_product_from_venue.html", {'form':form, 'submitted':submitted, 'product':product, 'venue_form':venue_form, 'data':data})

Venue_id (template)

<a href="{% url 'show-product-from-venue' product.id %}?next={{ request.path|urlencode }} method="POST">

Product_id(template with form)

<form action="{% url 'show-product-from-venue' product.id%}" method="POST">
    {% csrf_token %}
    {{ form}}
    <input type="submit" name="btnreview_form" name="next" value="{{ request.GET.next }}" class="btn btn-primary custom-btn">
</form>

urls

#VENUE PAGE
path('show_venue/<venue_id>', views.show_venue,name="show-venue"),

#PRODUCT
path('show_product_from_venue/<product_id>', views.show_product_from_venue,name="show-product-from-venue"),
PhilM
  • 249
  • 1
  • 11
  • Also share urls.py. – Sunderam Dubey Oct 07 '22 at 12:18
  • All done, just uploaded urls.py – PhilM Oct 07 '22 at 12:24
  • 1
    For example if a form comes from `http..../show_venue/23/` so after saving of form you want it to again go to same route with id=23 also, basically the same url, is that what you want? – Sunderam Dubey Oct 07 '22 at 12:26
  • Yes correct. Sorry, I should have made that clearer in my question. – PhilM Oct 07 '22 at 12:36
  • Sorry I misread your post, this is not what I am after. I am going to update my original post to make it clearer: user starts from http..../show_venue/23/, and will land on http..../show_product.../1/. The form is on http..../show_product.../1/. Once submitted, I want the user to be redirected to http..../show_venue/23/. – PhilM Oct 07 '22 at 13:54

1 Answers1

0

If you want to go previous pages then just add the root where exactly you want to go:

In views:

EX:

return HttpResponseRedirect("/users/")
Manoj Tolagekar
  • 1,816
  • 2
  • 5
  • 22
  • I considered that, but how would you redirect to an id specific url? ('show_venue/'). Obvioulsy the url would be different for each venue id, so I did not thing I could hard code the redirect hence the use of 'next'. – PhilM Oct 07 '22 at 11:45