我正在网站上制作搜索表单,通过该表单我们可以搜索特定的博客标题,直到现在我已经创建了表单:
<form action="GET" action="{% url 'posts:search' %}">
<input name="q" type="text" placeholder="Search" value="{{ request.GET.q }}">
<input type="submit">
</form>
urls.py中的网址:
url(r'^results/$', views.search, name='search'),
views.py下的功能:
def search(request):
query = request.GET.get('q')
posts = Posts.objects.filter(Q(title__icontains=query))
return render(request, 'posts/posts.html', {'posts': posts})
每当我单击搜索按钮时,它都会转到URL http://127.0.0.1:8000/GET/?q=lots+of
并给出错误Posts matching query does not exist.
错误的回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/GET/?q=lots+of
Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'posts']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Fruity_Dude\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\Fruity_Dude\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e,
request)
File "C:\Users\Fruity_Dude\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "C:\Users\Fruity_Dude\Projects\Django\devflow\posts\views.py" in
post_details
17. posts = Posts.objects.get(slug=slug)
File "C:\Users\Fruity_Dude\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args,
**kwargs)
File "C:\Users\Fruity_Dude\AppData\Local\Programs\Python\Python36\lib\site-
packages\django\db\models\query.py" in get
403. self.model._meta.object_name
Exception Type: DoesNotExist at /GET/
Exception Value: Posts matching query does not exist.
答案 0 :(得分:3)
您将表单提交到错误的网址http://127.0.0.1:8000/GET/?q=lots+of
,因为您有action="GET"
。
<form action="GET" action="{% url 'posts:search' %}">
将其更改为method="GET"
:
<form method="GET" action="{% url 'posts:search' %}">
错误消息表明您的post_details
视图无法处理不存在带有该条的帖子的情况:
def post_details(request, slug)
posts = Posts.objects.get(slug=slug) # can raise DoesNotExist
...
您可以改用get_object_or_404
:
from django.shortcuts import get_object_or_404
def post_details(request, slug)
posts = get_object_or_404(Posts, slug=slug)
...
最后,我建议将您的模型从Posts
重命名为Post
以匹配推荐的Django样式,并使用post = get_object_or_404(...)
,因为您要获取单个帖子。