What are the options when you want to return the user to the same page in Django and what are the pros/cons of each?
Methods I know:
Are there any other?
Use return redirect(request. META['HTTP_REFERER']) to redirect to previous url.
One can use the anchor tag to redirect to a particular section on the same page. You need to add ” id attribute” to the section you want to show and use the same id in href attribute with “#” in the anchor tag.
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.
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.
One of the way is using HTTP_REFERER
header like as below:
from django.http import HttpResponseRedirect def someview(request): ... return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Not sure of cons of this!
While the question and answer is old, I think it's lacking a few options. I have not find any cons with the methods, I would be happy to know if there are any?
request.build_absolute_uri()
from django.shortcuts import redirect redirect(request.path_info) # No query parameters redirect(request.build_absolute_uri()) # Keeps query parameters redirect(request.get_full_path()) # Keeps query parameters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With