NoReverseMatch at... error

Hi everyone,

I struggled with this error for several days and couldn’t find a solution.

error message:

# NoReverseMatch at /search/

Reverse for 'search-results' with arguments '('good book',)' not found. 1 pattern(s) tried: ['results/\\Z']

|Request Method:|POST|
| --- | --- |
|Request URL:|http://127.0.0.1:8000/search/|
|Django Version:|5.0.6|
|Exception Type:|NoReverseMatch|
|Exception Value:|Reverse for 'search-results' with arguments '('good book',)' not found. 1 pattern(s) tried: ['results/\\Z']|
|Exception Location:|C:\Users\dady\PycharmProjects\rp-portfolio\venv\lib\site-packages\django\urls\resolvers.py, line 851, in _reverse_with_prefix|
|Raised during:|search.views.search_main|

main urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('books.urls')),
    path('projects/', include('projects.urls')),
    path('books/', include('books.urls')),
    path('bands/', views.band_list, name='band-list'),
    path('bands/<int:band_id>/', views.band_detail, name='band-detail'),
    path('bands/add/', views.band_create, name='band-create'),
    path('bands/<int:band_id>/change/', views.band_edit, name='band-edit'),
    path('bands/<int:band_id>/delete/', views.band_del, name='band-del'),
    path('about-us/', views.about),
    path('songs/', views.songs_list, name='songs-list'),
    path('songs/<int:song_id>/', views.song_detail, name='song-detail'),
    path('songs/add/', views.song_create, name='song-create'),
    path('songs/<int:song_id>/change/', views.song_edit, name='song-edit'),
    path('songs/<int:song_id>/delete/', views.song_del, name='song-del'),
    path('contact-us/', views.contact, name='contact'),
    path('email-sent/', views.email_sent, name='sent-e'),
    path('', include('search.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

search urls.py

urlpatterns = [
    path('search/', views.search_main, name='search-main'),
    path('results/', views.search_results, name='search-results'),
]

search views.py

from django.shortcuts import render
from django.shortcuts import redirect
from django.shortcuts import HttpResponse
from search.Tzomet_script import tzomet

def search_main(request):
    if request.method == 'POST':
        book_name = request.POST.get('book_name')
        #results = tzomet(book_name)
        results = 'good book'
        print(results)
        #return HttpResponse(f'{results}')
        return redirect('search-results', results)
    else:
        return render(request, 'search/search_main.html')


def search_results(request, book_details):
    return render(request, 'search/search_results.html', {'book_details': book_details})

Any help will be appreciated.

Welcome @Ddady1 !

You are passing a parameter to the redirect function that will try to reverse the search-results-url.

However:

Your search-results url is not defined to accept any arguments.

You are either reversing the wrong url, or you have defined your search-results url incorrectly. (You haven’t posted your search_results view, so I can’t tell which case applies or what the correct solution would be.)

Thanks, KenWhitesell for your reply.

Did you mean by saying

(You haven’t posted your search_results view, so I can’t tell which case applies or what the correct solution would be.)

my search_result.html? because I posted my search_results code in ‘search/view.py’.
Anyway, here is the search_results.html (it’s just a basic for now, to see that it works)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{ book_details }}
</body>
</html>

if that’s now the file you were asking, I’ll appreciate if you’ll explain which file.

No, I’m not talking about the template, I’m talking about the view.

do you mean this search file?

Yes, I missed this in the earlier post:

You have a parameter that search_results is expecting (book_details), but you don’t have a parameter named book_details in your url definition.

Add a URL for passing arguments to the URL pattern, or
Set the redirect URL in a different way.

Add a URL

# search urls.py
urlpatterns = [
    path('search/', views.search_main, name='search-main'),
    path('results/', views.search_results, name='search-results'),
    path('results/<str:book_details>/', views.search_results, name='search-results'), # add
]

or Set the redirect URL

# search views.py
def search_main(request):
    if request.method == 'POST':
        book_name = request.POST.get('book_name')
        #results = tzomet(book_name)
        results = 'good book'
        print(results)
        # return HttpResponse(f'{results}')
        # return redirect('search-results', results)
        url = {url}
        return redirect(url)
    else:
        return render(request, 'search/search_main.html')

Aside from that first quoted line being syntactically incorrect, this is incorrect because the view is defined with a required argument. Invoking the view without providing the parameter is an error.

Of course, this part was written to fill in the command to find the URL.
I’m not saying you should just use it as is. as You know it.

Thank you ken and white,

i add the line:

and it’s working great.

But the real value of the variable ‘book_details’ is a dictionary and not just a 'good book" which was only for testing matters.
I changed the code from 'results/<str:book_details>/' to 'results/<dict:book_details>/' and I’m getting again the NoReverseMatch error. I tried with ‘str’ and still error.
What am I doing wrong now? :thinking:

you must see this.