gpt4 book ai didi

python - django 使用原始 mysql 进行搜索

转载 作者:行者123 更新时间:2023-11-29 10:36:24 25 4
gpt4 key购买 nike

我正在尝试创建一个具有 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 中。请帮助我修复此搜索引擎预先感谢您

最佳答案

q

您需要处理 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,但您正在 request.GET 中查找 q。您需要将输入命名为“q”或在 request.GET

中查找“Title”
q = request.GET.get("Title")

关于python - django 使用原始 mysql 进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46345254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com