Reputation: 215
I have a wishlist system. By this, user can add items to wishlist by clicking on heart icon, the problem is that i have two places where user can remove the items from wishlist:
1. User can remove items on the wishlis from the search results
2. User can remove items on the wishlis from "Wishlist" menu
So by this, how to redirect based on page for example,
1, When user reclicks heart icon on search results it do action and redirect to same search result page
2, And when user reclicks heart icon on wishlist menu it should redirect back to wishlist again
Here is the code
def save_wish(request, pk):
if request.method == 'POST':
student = request.user.student
.........
.........
return redirect('students:search')
So the above code always returns to "search" irrespective the page user clicked, how to conditional redirect to "Search" when user clicks from search and to "wishlist" when user clicks from "wishlist" page?
Upvotes: 0
Views: 531
Reputation: 116
One way to do this is using hidden field in your form. When removing an item on the wishlist from the search result, you can use a hidden field inside the form-
<input type="hidden" id="hidField" name="hidField" value="searchPage">
Then in views-
def save_wish(request):
if request.method=="POST":
hidden_value=request.POST["hidField"]
---------------------------------
if hidden_value=="searchPage":
return redirect("students:search")
else:
return redirect("wishlist-page")
Upvotes: 1
Reputation: 46
You could use:
request.path # -without GET parameters request.get_full_path
I.e. :
def save_wish(request, pk):
if request.method == 'POST':
student = request.user.student
.........
.........
if request.path == '/search':
return redirect('students:search')
elif:
return redirect('/whishilit-url')
More elegant approach would be:
request.resolver_match.view_name
I.e.:
def save_wish(request, pk):
route = request.resolver_match.view_name
if request.method == 'POST':
student = request.user.student
.........
.........
return redirect(route)
Upvotes: 1
Reputation: 51
Two ways I can think of solving this:
request.META.HTTP_REFERER
from django.http import HttpResponseRedirect
def someview(request):
...
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
path_info
from django.http import HttpResponseRedirect
def someview(request):
...
return HttpResponseRedirect(request.path_info)
Upvotes: 1