NoReverseMatch at /logout

NoReverseMatch at /logout

Reverse for ‘home’ not found. ‘home’ is not a valid view function or pattern name.
i am getting this error how can i overcome this error!

here is my views for logout. I

    logout(request)
    messages.info(request, 'you have logout successfully')
    return redirect('home')

in template

<li class="nav-item">
     <a class="nav-link" href="{% url 'webapp:logout' %}"><h class="" style=" font-family: 'Pacifico', cursive;">Login</h></a>
</li>

in urls.py

path('logout', views.log_out, name = 'logout'),

See the docs on redirect - ‘home’ would need to be the name of a view in your urls.py file.

I read the documentation and i also realized my mistakes. but i didnt understand the documentation.

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')

what is mean by

foo='bar'

From the docs:

  • A view name, possibly with arguments: reverse() will be used to reverse-resolve the name.

So in this example, foo would be the name of an argument in the url. You would expect to find a url that looks like this:

path('some/url/<str:foo>/', views.some_view, name='some-view-name')

i am not understanding how can implement this code in my project.

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')

in the above code there is view name with arguments. I have a view name ‘index’ which will render to home page of my website. here is my index view in views.py

def index(request):  #home
    texts = Homepage.objects.all()
    videos = Video.objects.all()
    context = {'texts': texts, 'videos': videos}
    return render(request, 'webapp/index.html', context)

so i pass this view name in my another view log_out

def log_out(request):
    logout(request)
    messages.info(request, 'you have logout successfully')
    return redirect('index', foo='bar')# i may change this argument later

after this what should i do should i create a url for accepting argument ‘foo’
to take that ‘foo’ argument should i create another function view!!!
oh! i dont know how is this working.

You don’t redirect to a view function. You redirect to the name attribute of the path in your urls.py file.