0

Like many other questions on here (Writing a very basic search form in Django, django search page not found error), I am using Django and I have a search bar in a form that when submitted, returns a "Page not found" error. Getting the urls correct tends to be my achilles and I am hoping an extra set of eyes can spot my error.

Here are excerpts of my code:

models.py

class Tool(models.Model):
    tool_name = models.CharField(max_length=200)
    release_date = models.DateField('release_date', null=True, blank=False)

views.py

def search_results(request):
   if request.GET:
      search_term = request.GET['search_box']
      results = Tool.objects.filter(tool_name__icontains=search_term)

      return render(request, 'search_results.html', {'results': results})
   return render(request, 'search_results.html')

urls.py

urlpatterns = [
   ...
   path('search_results/', views.search_results, name='search_results'),
]

templates/base_generic.html

<form name="search_form" method="GET" action="{ % url 'search_results' %}">
   <input  id="search_box" type="text" name="search_box"  placeholder="Search tools..." >
   <button id="search_submit" type="submit" >Submit</button>
</form>

templates/search_results.html

{% extends "base_generic.html" %}
...
{% if results %}

   <table style="width:100%">
      <tr>
         <th>Tool</th>
         <th>Release Date</th>
      </tr>

      {% for t in results %}
         <tr>
            <td><a href="{% url 'tool-detail' t.tool_name.pk %}">{{ t.tool_name }}</a> </td>
            <td>{{ t.release_date }}</p>
         </tr>
      {% endfor %}
   </table>
   <hr><p>
   {% else %}
      <h3 class='error'>Your search term returned no items, please try again.</h3>
   {% endif %}

{% endblock %}

file structure

├── models.py
├── templates
│   ├── base_generic.html
│   ├── index.html
│   ├── packages
│   │   ├── developer_detail.html
│   │   ├── developer_list.html
│   │   ├── publication_detail.html
│   │   ├── publication_list.html
│   │   ├── tool_detail.html
│   │   └── tool_list.html
│   ├── search_results.html
├── urls.py
└── views.py

Error


Page not found (404)
Request Method:     GET
Request URL:    http://localhost:8000/packages/%7B%20%25%20url%20'search_results'%20%25%7D?search_box=Scampy

Using the URLconf defined in Bio.urls, Django tried these URL patterns, in this order:

    admin/
    packages/ [name='index']
    packages/ tools/ [name='tools']
    packages/ tool/<int:pk> [name='tool-detail']
    packages/ developers/ [name='developers']
    packages/ developer/<int:pk> [name='developer-detail']
    packages/ publications/ [name='publications']
    packages/ publication/<int:pk> [name='publication-detail']
    packages/ new_developer/ [name='new_developer']
    packages/ new_tool/ [name='new_tool']
    packages/ thanks/ [name='thanks']
    packages/ signup/ [name='signup']
    packages/ accounts/
    packages/ search_list/ [name='search_list']
    packages/ search_results/ [name='search_results']
    accounts/
    ^static/(?P<path>.*)$

The current path, packages/{ % url 'search_results' %}, didn't match any of these.

If anyone with insight into my error could assist me I would greatly appreciate it.

cer
  • 1,961
  • 2
  • 17
  • 26
  • 1
    The problem is the space between the curly bracket and percent sign in `action="{ % url 'search_results' %}">`. Change it to `action="{% url 'search_results' %}">` – Alasdair Jul 07 '20 at 18:06
  • YEP! That was it, thank you @Alasdair. I am more than happy to accept it if you want to put that into an answer. – cer Jul 07 '20 at 18:10

1 Answers1

0

When you are including search.urls in your project urls.py are u using namespace? Something like this

path('packages/',include("search.urls",namespace='search')),

if yes u should use method="GET" action='{% url "search:search_results"%}'

in general namespace:name

Archi
  • 1
  • 1