0

I need to get data from a redirect, but I can't really figure out how. The method is GET as I can see from my print. But I have tried everything I found on searching for this without luck. What am I doing wrong? I really appreciate any help.

I have this redirect:

return redirect('/list-view', new = 'new')

My urls looks like this:

path('list-view/<new>', views.list_view, name='list'),

Then my list-view is:

def list_view(request, *args, **kwargs):
  print(request.method)
  if request.method == 'GET':
      aa=request.GET.get('new')
      if aa:
          bb = (request.GET.get('new'))
          print (bb['new'])
Peter
  • 43
  • 6
  • I think ```return redirect()``` wiil put arguments in your ```urls``` and not sending back list of items for showing in template. Do you need to pass arguments to ```urls``` only ? – iliya Jun 05 '20 at 14:22
  • I really don't care how the values are sent as long as I can get them in my next view. It is just a parameter telling me if it is a new product or not. Do you have a better idea to send data so please let me know. :-) – Peter Jun 05 '20 at 14:31
  • Actually, you gave me an idea that works. return redirect('/list-view?new=new') – Peter Jun 05 '20 at 14:40

2 Answers2

1

If you define a parameter in the url, like you are doing, you can actually put it as a input on the view function,

def list_view(request, new):
  # ...

BTW to use the name you need to reverse it, something like this,

new = True
return HttpResponseRedirect(reverse('list', args=(new,)))
cabesuon
  • 4,860
  • 2
  • 15
  • 24
0

Use the name of the URL instead of the absolute path. return redirect('list', new = 'new')

ludel
  • 91
  • 5