我正在尝试使用4个搜索字段创建自定义搜索引擎。我想在mysql表中搜索。
这是我的views.py中搜索,分页的一部分,它列出了整个表数据。
def addview(request, table_id):
try:
table_name = Crawledtables.objects.get(id=table_id)
tbl_details = "SELECT * FROM " + table_name.name
tbl_detail = AllTables.objects.raw(tbl_details)
paginator = Paginator(list(tbl_detail), 100)
page = request.GET.get('page')
try:
details = paginator.page(page)
except PageNotAnInteger:
details = paginator.page(1)
except EmptyPage:
details = paginator.page(paginator.num_pages)
q = request.GET.get("q")
title_search = """SELECT * FROM """ + table_name + """ WHERE `Title` LIKE '\%""" + q + """\%' """
# title_search = """SELECT id,description, MATCH (title) AGAINST (""" + q + """ IN BOOLEAN MODE) " \
# FROM """ + table_name + """ ORDER BY id DESC;"""
search_title = AllTables.objects.raw(title_search)
return render(request, 'tables/table_list.html', {'tbl_name': table_name,
'details': tbl_detail,
'search': search_title,
'detail_page': details})
except AllTables.DoesNotExist:
raise Http404()
这是我的models.py的一部分,包含动态表类。
@python_2_unicode_compatible
class AllTables(models.Model):
title = models.TextField(db_column='Title', blank=True, null=True)
url = models.CharField(db_column='Url', unique=True, max_length=250, blank=True,
null=True)
description = models.TextField(db_column='Description', blank=True, null=True)
class Meta:
managed = False
def __str__(self):
return self.url
def __unicode__(self):
return self.title
我使用views.py中的任何 title_search 获得的错误
Traceback (most recent call last):
File "/home/omega/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/omega/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/omega/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/tester/tables/views.py", line 52, in addview
title_search = """SELECT * FROM """ + table_name + """ WHERE `Title` LIKE '\%""" + q + """\%' """
TypeError: coercing to Unicode: need string or buffer, NoneType found
我一直试图让这个搜索工作过去一周,并且我一直在询问它...使用不同类型的方法。例如:elasticsearch,haystack等......
更新
我修改了views.py就像你说的那样。而且我没有得到更多错误......但我也没有得到任何结果。
这是我的 table_list.html
的一部分 <h3>Search Engine</h3>
<form class="navbar-form navbar-left" role="search" method="get" action="{% url 'tables:details' table_id=tbl_name.id%}">
<div class="form-group">
<input placeholder="Title" type="text" class="form-control" name="Title" value="">
<input placeholder="Url" type="text" class="form-control" name="Url" value="">
<input placeholder="Description" type="text" class="form-control" name="Description" value="">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
<p>You searched for open sections of: <strong>{{ query }}</strong></p>
{% if search %}
<p>Found {{ search|length }} section{{ search|pluralize }}.</p>
<ul>
{% for section in search %}
<li>{{ section.title }}</li>
<li>{{ section.url }}</li>
<li>{{ section.description }}</li>
{% endfor %}
</ul>
{% else %}
<p>No open sections found.</p>
{% endif %}
我只做了title_search ...在我得到标题修复后我将要做的另一个..他们就在html中。 请帮助我修复此搜索引擎 提前谢谢
答案 0 :(得分:1)
q
是None
您需要处理q
request.GET
的情况
q = request.GET.get("q")
if q is not None:
title_search = """SELECT * FROM """ + table_name + """ WHERE `Title` LIKE '\%""" + q + """\%' """
# title_search = """SELECT id,description, MATCH (title) AGAINST (""" + q + """ IN BOOLEAN MODE) " \
# FROM """ + table_name + """ ORDER BY id DESC;"""
search_title = AllTables.objects.raw(title_search)
else:
search_title = []
您的输入名称为Title
,但您正在q
中寻找request.GET
。您需要命名输入&#34; q&#34;或寻找&#34;标题&#34;在request.GET
q = request.GET.get("Title")