1

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#

Hunter
  • 646
  • 1
  • 6
  • 23
  • This `except ObjectDoesNotExist: pass` will leave the `context` dictionary empty on error, without any "cameras" key. – C14L Jun 18 '19 at 13:53
  • You're probably right and I'll update that, but when I print `context` for `jobsite9` it retrieves the cameras i'm looking for and throws the error. Actually, it appears that I had a commented out url tag in my HTML file that was being read by Django. Why does Django read commented out pieces of HTML? – Hunter Jun 18 '19 at 13:57

2 Answers2

1

try giving the syntax like this in the templates

             **"{% url 'appname:search' %}"**

this may work

0

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.

nigel222
  • 7,582
  • 1
  • 14
  • 22
  • I am extending the base template. I think I just found what was wrong. I had `` in my siren_search.html file, and I think Django was reading that, because I just removed that piece of code and it started working again. I suppose this now raises the question, why does Django read commented out HTML? – Hunter Jun 18 '19 at 13:59
  • It can be an useful debugging technique to put `{{whatever}}` in an HTML comment and then use "view page source" in your browser. So I'm glad it does. – nigel222 Jun 18 '19 at 14:03
  • 1
    You need to use [Django comments](https://stackoverflow.com/a/719917/113962) if you want the `{% url %}` tag to be ignored. Django doesn't parse the template as html - remember a rendered Django template doesn't necessarily return html, for example it could return plain text for an email message. – Alasdair Jun 18 '19 at 14:04
  • 1
    More generally, the Django template language is not restricted to HTML. I've occasionally substituted into Javascript. Others have used it to generate `.csv` files, or even PostScript. – nigel222 Jun 18 '19 at 14:04