Jack Clarkson
Jack Clarkson

Reputation: 105

django basic search function saying NoReverseMatch

I'm trying to follow this tutorial to make a todoapp and if i search something its saying NoReverseMatch at /todoapp/ with Reverse for 'search' not found. 'search' is not a valid view function or pattern name.

here's my views.py

def searchtodolist(request):
    if request.method == 'GET':
        query = request.GET.get('content', None)
        if query:
            results = Todoitem.objects.filter(content__contains=query)
            return render(request, 'todoapp.html', {"results": results})
    return render(request, 'todoapp.html')

and here's the urls.py

urlpatterns = [
    # path('url path, views.py function names)
    # ex. 127.0.0.1:8000/admin/
    path('addTodo/', addTodo),
    path('admin/', admin.site.urls),
    path('deleteTodo/<int:todo_id>/', deleteTodo),
    path('search/', searchtodolist),
]

and lastly my todoapp.html

<body>
<br>
<form action="{% url 'search' %}" method="get">{%csrf_token%}
<input type="text" name="content" placeholder="Search a todolist" class="form-control">
<input type="submit" name="submit" value="Search"/>
</form>
<table>
<tr>
    <th colspan="2">List of Todos</th>
</tr>
{% for result in results %}
<tr>
    <td>{{result.content}}</td>
    <td><form action="/deleteTodo/{{todo_items.id}}/" style="display: inline; " method="post"> 
{%csrf_token%}
    <input class="button button1" type="submit" value="Delete"/>
    </form></td>
</tr>
{% endfor %}
</tr>
</table>
</body>

Upvotes: 0

Views: 32

Answers (1)

Amin
Amin

Reputation: 2853

You should give url a name for being able to reverse it with it's name

So use

    path('search/', searchtodolist, name="search")

This gives your /search/ url a name called "search". and you can then access to this url via {% url 'search' %}

Notice that if you have declared app_name in your urls.py, you should specify that in url address name

assume urls.py sth like:

app_name = "test_app"
urlpatterns = [
    ...
]

Then you should use {% url 'test_app:search' %}

Upvotes: 1

Related Questions