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#