简体   繁体   English

如何在帖子中进行搜索(在Django中)

[英]how to make a search in post (in django)

I am working on making a search form in the website through which we can search for a particular blog title till now I have created a form: 我正在网站上制作搜索表格,通过该表格我们可以搜索特定的博客标题,直到现在我已经创建了一个表格:

<form action="GET" action="{% url 'posts:search' %}">
    <input name="q" type="text" placeholder="Search" value="{{ request.GET.q }}">
    <input type="submit">
</form>

url for that in urls.py: urls.py中的网址:

url(r'^results/$', views.search, name='search'),

function under views.py: 在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})

Whenever I click on search button it goes to the URL http://127.0.0.1:8000/GET/?q=lots+of and gives the error Posts matching query does not exist. 每当我单击搜索按钮时,它都会转到URL http://127.0.0.1:8000/GET/?q=lots+of ,并显示错误Posts matching query does not exist.

traceback for the error: 错误的回溯:

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.

You are submitting the form to the wrong URL http://127.0.0.1:8000/GET/?q=lots+of because you have action="GET" . 您将表单提交到错误的URL http://127.0.0.1:8000/GET/?q=lots+of因为您有action="GET"

<form action="GET" action="{% url 'posts:search' %}">

Change it to method="GET" : 将其更改为method="GET"

<form method="GET" action="{% url 'posts:search' %}">

The error message shows that your post_details view cannot handle the case where a post with that slug does not exist: 错误消息显示,您的post_details视图无法处理不存在带有post_details的帖子的情况:

def post_details(request, slug)
    posts = Posts.objects.get(slug=slug)  # can raise DoesNotExist
    ...

You could use get_object_or_404 instead: 您可以改用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)
    ...

Finally, I would recommend renaming your model from Posts to Post to match the recommended Django style, and use post = get_object_or_404(...) since you are fetching a single post. 最后,我建议将模型从Posts重命名为Post以匹配推荐的Django样式,并使用post = get_object_or_404(...)因为您正在获取单个post。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM