【发布时间】:2016-04-05 21:59:47
【问题描述】:
我一直在尝试为博客项目实现搜索功能,现在各种教程中的几种不同实现都一致地出现 404,无论输入如何,我都在这里寻找答案。
这是对我来说最基础的,当然是views.py:
def search(request):
try:
q = request.GET['q']
posts = Post.objects.filter(title__search=q)
return render_to_response('blog/search_post_list.html', {'object_list': posts, 'q':q})
except KeyError:
return render_to_response('blog/search_post_list.html')
在blog/urls.py:
url(r'^search', search, name='search'),
/templates/blog/search_post_list.html 有一个模板,其中包含一些包含{% for post in object_list %} 的代码,与工作模板相同。
所以,去任何localhost:8000/blog/search?q=<search_query_here> 都是一个带有 404 的 Django 调试页面。
我让代码保持简单的原因是因为我感觉代码之外可能还有一些东西,我希望有人能告诉我在哪里查找代码。
编辑: 这是 404 页面:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/blog/search?q=what
Raised by: django.views.generic.detail.DetailView
No post found matching the query
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
这是我的 DetailView 的 URL,因为它显然被调用了。
url(r'^(?P<slug>[a-zA-Z0-9-]+)/?$',
DetailView.as_view(
model=Post,
)),
【问题讨论】:
-
404调试页面的全文是什么?
-
@Alasdair
Page not found (404) Request Method: GET Request URL: http://localhost:8000/blog/search?q=what Raised by: django.views.generic.detail.DetailView No post found matching the query You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. -
我现在意识到我可能需要为我的 DetailView 放置 url。添加。
标签: python django sqlite querying