qmayo14
qmayo14

Reputation: 11

Cannot fix Django NoReverseMatch error despite URL being withing urls.py

Before I go into detail, here is the line from the html document that results in the error, as well as views.py and urls.py:

<input class="search" type="text" name="q" placeholder="Search Encyclopedia" action="{% url 'encyclopedia:findpage' %}" method="get">

(Yes there is a space betweeen url and 'encyclopedia:findpage')

views.py findpage function:

def findpage(request, name):
    pages = util.list_entries()
    wanted_pages = set()
    wanted_page = None

    for page in pages:
        if not wanted_page and page == name:
            wanted_page = util.get_entry(name)
            continue

        if name in page:
            wanted_pages.add(page)

    if wanted_page:
        return render(request, 'encyclopedia/page.html', {
            "title": name,
            "entry": wanted_page
            })

    if wanted_pages and not wanted_page:
        return render(request, 'encyclopedia/index.html', {
            "entries": wanted_pages
        })

    else:
        return render(request, 'encyclopedia/noresults.html')

urls.py:

from django.urls import path

from . import views

app_name = "encyclopedia"
urlpatterns = [
path("", views.index, name="index"),
path("<str:name>", views.wikipage, name="wikipage"),
path("<str:name>", views.findpage, name='findpage'),
path("create", views.newpage, name="newpage")

]

When I run the Django project, I get a NoReverseMatch at /. The error from the webpage reads: In template /home/quinn/Desktop/cs50web/pset1/wiki/encyclopedia/templates/encyclopedia/layout.html, error at line 17 Reverse for 'findpage' with no arguments not found. 1 pattern(s) tried: ['(?P<name>[^/]+)$']

Line 17 is the piece of html I left at the top of this page. I've checked numerous sources in an attempt to fix this, but I cannot figure it out.

Upvotes: 0

Views: 43

Answers (1)

JLeno46
JLeno46

Reputation: 1269

You need to specify a name because your url needs it here:

path("<str:name>", views.findpage, name='findpage')

You can specify kwargs in the url tag in your template like this:

{% url 'reverse:lookup' kwarg='insert here the value that needs to get passed in your url' %}

Upvotes: 1

Related Questions