我正在尝试使用Haystack-Whoosh在Django应用程序中进行搜索。我已经实现了与文档页面中提到的代码相同的代码:django-haystack documentaation 但仍然无效,搜索结果未被过滤。
这是我的代码:
models.py
from django.db import models
from datetime import datetime
class Newcar(models.Model):
car_name = models.CharField(max_length=50)
carmodel = models.CharField(max_length=50)
car_logo = models.CharField(max_length=1000)
pub_date = models.DateTimeField(default=datetime.now())
def __str__(self):
return self .car_name + '-' + self .carmodel
search_indexes.py
import datetime
from haystack import indexes
from car.models import Newcar
class NewcarIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
car_name = indexes.CharField(model_attr='car_name')
carmodel = indexes.CharField(model_attr='carmodel')
pub_date = indexes.DateTimeField(model_attr='pub_date')
def get_model(self):
return Newcar
def index_queryset(self, using=None):
return self.get_model().objects.all()
newcar_text.txt
{{ object.car_name }}
{{ object.carmodel }}
search.html
{% block content %}
<h2>Search</h2>
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>
{% if query %}
<h3>Results</h3>
{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object.car_name }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}<a href="?q={{ query }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% else %}
{% endif %}
</form>
{% endblock %}
这是我得到的错误:
File "C:\Users\hp\ENV\lib\site-packages\django\core\servers\basehttp.py", line 88, in handle_error
super(ServerHandler, self).handle_error()
File "c:\users\hp\appdata\local\programs\python\python36\Lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "c:\users\hp\appdata\local\programs\python\python36\Lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "c:\users\hp\appdata\local\programs\python\python36\Lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "c:\users\hp\appdata\local\programs\python\python36\Lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "c:\users\hp\appdata\local\programs\python\python36\Lib\wsgiref\handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\users\hp\appdata\local\programs\python\python36 \Lib\socketserver.py", line 639, in process_request_thread
self.finish_request(request, client_address)
File "c:\users\hp\appdata\local\programs\python\python36\Lib\socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "c:\users\hp\appdata\local\programs\python\python36\Lib\socketserver.py", line 696, in __init__
self.handle()
File "C:\Users\hp\ENV\lib\site-packages\django\core\servers\basehttp.py", line 155, in handle
handler.run(self.server.get_app())
File "c:\users\hp\appdata\local\programs\python\python36\Lib\wsgiref\handlers.py", line 144, in run
self.close()
File "c:\users\hp\appdata\local\programs\python\python36\Lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'