Django搜索返回“找不到页面(404)”错误

时间:2020-07-07 17:56:36

标签: django http-status-code-404 submit

与此处的其他许多问题(Writing a very basic search form in Djangodjango search page not found error)一样,我使用的是Django,并且我有一个搜索栏,该表单的格式在提交时会返回“找不到页面”错误。正确设置网址通常是我的致命弱点,我希望能有更多的机会发现我的错误。

以下是我的代码的摘录:

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 %}

文件结构

├── 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

错误


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.

如果任何了解我的错误的人都能帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

当您在项目urls.py中包含search.urls时,您是否使用名称空间? 像这样

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

如果是,则应使用method="GET" action='{% url "search:search_results"%}'

一般namespace:name