0

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?

sixovov947
  • 205
  • 1
  • 7
  • Just create two views and use to urls to separate the logic. Alternatively create another if-else construct in the view above to have to redirect options. Also don't name your function to remove a wish `save_wish, this might lead to confusion sooner or later – JSRB Sep 08 '21 at 06:04

3 Answers3

1
  1. 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')
  1. 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)
Nihad
  • 50
  • 7
  • actually its a different view name ! Saving data is in different view and redirect should be on different view ! How to change the view name ? – sixovov947 Sep 08 '21 at 09:03
  • Just add a simple if conditions, and match the "route" variable. One approach, if you have only two routes you wanna redirect is ternary operator: route = 'view-name1' if request.resolver_match.view_name=='view-name2' else 'view-name2'. for multiple views, use if/else . – Nihad Sep 08 '21 at 09:14
  • I had problem, as it only show the view from which it gets actioned ! so used @Md Lutfor Rahman answer ! Thanks for spending time – sixovov947 Sep 08 '21 at 11:23
1

Two ways I can think of solving this:

  1. Using request.META.HTTP_REFERER
from django.http import HttpResponseRedirect

def someview(request):
   ...
   return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

[Original answer]

  1. Using path_info
from django.http import HttpResponseRedirect

def someview(request):
   ...
   return HttpResponseRedirect(request.path_info)

[Original answer]

Sakshi
  • 51
  • 6
1

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")
  • Cant the hidden field be manipulated by inspect elements ? – sixovov947 Sep 08 '21 at 09:01
  • Yes, it can be manipulated. It works in the same way as redirect to requested page after login. When someone deteriorates the value purposely he should not expect to redirect to the right page. – Md Lutfor Rahman Sep 08 '21 at 09:28