[英]Django search returns "Page not found (404) error
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.像这里的许多其他问题一样( 在 Django 中编写一个非常基本的搜索表单, django 搜索页面未找到错误),我正在使用 Django 并且我在提交时返回“未找到页面”错误。 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模型.py
class Tool(models.Model):
tool_name = models.CharField(max_length=200)
release_date = models.DateField('release_date', null=True, blank=False)
views.py视图.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网址.py
urlpatterns = [
...
path('search_results/', views.search_results, name='search_results'),
]
templates/base_generic.html模板/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模板/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.如果任何了解我的错误的人可以帮助我,我将不胜感激。
When you are including search.urls in your project urls.py are u using namespace?当您在项目 urls.py 中包含 search.urls 时,您是否使用命名空间? Something like this
像这样的东西
path('packages/',include("search.urls",namespace='search')),
if yes u should use method="GET" action='{% url "search:search_results"%}'
如果是,你应该使用
method="GET" action='{% url "search:search_results"%}'
in general namespace:name
一般
namespace:name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.