Reputation: 666
This is yet another question involving paths in Django. I have not been able to find my answer anywhere and have done lots of searching on this.
The return()
function in my view is throwing the error
django.urls.exceptions.NoReverseMatch: Reverse for '' not found. '' is not a valid view function or pattern name.
Here is my code.
<!-- siren_search.html -->
<div class="row">
<div class="col-sm-8 col-md-7 col-xl-5 mx-auto">
<form id="searchform" action="{% url 'search' %}" method="GET">
<input id="searchbar" name="query" autocomplete="on" onkeyup=getCameras(this.value)
placeholder="Search for the name of a jobsite." class="form-control" type="search" />
</form>
</div>
</div>
#### urls.py
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', views.siren_home, name = 'siren_home'),
re_path(r'^search/$',views.search, name = 'search')
]
#### views.py
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.core.exceptions import ObjectDoesNotExist
from .models import CameraSystem, CameraModel, ControlByWeb, JobSite
from django.core import serializers
import json
def siren_home(request):
# some functionality
return render(request, 'siren_search.html', context)
def search(request):
term = request.GET.get('query')
context = {}
# Handle when the user presses enter on the search bar
if 'query' in request.GET and term != '' and not request.is_ajax():
try:
jobsite = JobSite.objects.get(name__iexact = term)
cameras = jobsite.camerasystem_set.all()
context = {
'cameras': cameras,
}
except ObjectDoesNotExist:
pass
return render(request, 'siren_search.html', context) # Django fails here
else:
return render(request, 'siren_search.html', context)
When I hit enter on the search bar it will route to the proper view function and do all the necessary computations, but it fails on the render() function. The url I have in my browser is: http://localhost:8000/siren-search/search/?query=jobsite9
.
Here is a link to my traceback: http://dpaste.com/2KFAW9M#
Upvotes: 1
Views: 3394
Reputation: 9
try giving the syntax like this in the templates
**"{% url 'appname:search' %}"**
this may work
Upvotes: 1
Reputation: 8192
Really only merits a comment, but I can't format this there.
In the traceback you link to,
Template error:
In template /Users/name/Programming/test/webapp/webapp/templates/base.html, error at line 37
Reverse for '' not found. '' is not a valid view function or pattern name.
which suggests to me that you may be looking in the wrong place, and indeed that the problem might not even be in code that you have written. Look at line 37 of base.html, and see if it depends on something that you haven't passed in your context. Or try stubbing out base.html for now.
I'm puzzled though, because siren_search.html as posted does not show that it is extending any base template.
Upvotes: 0