Search code examples
python-3.xdjango-viewsdjango-urlsdjango-3.0django-url-reverse

Django Python 3.* : Reverse for 'view' not found


I have a first_view that takes id argument, And second_view that redirects to first_view after interpreting some code.

views.py:

def first_view(request, id):
    #do something
    return render(request, 'sometemplate.html')

def second_view(request, id):
    #do something
    return redirect('first_view', id=id)

when I try to go through second_view I get this error:

Reverse for 'first_view' not found.

urls.py:

    path('first_view/<int:pnr_id>/', views.first_view, name='first_view'),
    path('second_view/<int:pnr_id>/', views.second_view, name='second_view'),

I've tried these syntax for redirect too:

  1. return redirect('first_view', id)
  2. return redirect('first_view', args=[id])
  3. return redirect('first_view', args=[id=id])
  4. return redirect(first_view(id=id))
  5. return redirect(first_view(id))
  6. return redirect(first_view(request=request,id=id))
  7. return redirect(first_view(request,id))

Nothing works! any support will be appreciated.


Solution

  • here is the issue to save your time..

    when you call redirect() make sure that you are passing the url name not the view def name