gpt4 book ai didi

python - Django 多个搜索词

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:22 25 4
gpt4 key购买 nike

我想添加 1 个算法中的所有列以进行搜索。如果可以的话。

类似这样的事情:

*更新*(我已经更新了views.py和search_table.html)它只是正确搜索 url 字段。我在这些字段中输入的 ID 和标题将提供整个表格。

views.py

def search_table(请求, pk): table_name = Crawledtables.objects.get(id=pk) t = create_model(表名.name)

q = request.GET['q']
if q is not None:

query = t.objects.filter(Q(id__icontains=q) | Q(title__icontains=q) | Q(url__icontains=q))
return render(request, 'search/results_table.html', {'tbl_name': table_name,
'details': query,
'query': q})

else:
return HttpResponse("Please submit a search term!")

results_table.html

<strong> {{ tbl_name }}</strong>
<p> You searched for: <strong>{{ query }}</strong></p>
{% if details %}
<p> Found {{ details|length }}</p>
<div class="row">

<table class="table table-bordered sortable">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Url</th>
</tr>
</thead>
<tbody>
{% for lists in details %}
<tr>
<td>{{ lists.id }}</td>
<td>{{ lists.title }}</td>
<td><a href="{{ lists.url }}" target="_blank">{{ lists.url }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

{% else %}
<p> No results found</p>
{% endif %}

{% endblock %}

search_table.html

{% if tbl_name %}
<form action="/search/{{ tbl_name.id }}/results" method="GET">
{% endif %}
<input type="text" name="q" placeholder="Id">
<input type="text" name="q" placeholder="Title">
<input type="text" name="q" placeholder="Url">
<input type="submit" value="Search">
</form>

更新

models.py

def create_model(db_table):
class CustomMetaClass(ModelBase):
def __new__(cls, name, bases, attrs):
model = super(CustomMetaClass, cls).__new__(cls, name, bases, attrs)
model._meta.db_table = db_table
return model

class AllTables(models.Model):
__metaclass__ = CustomMetaClass

id = models.IntegerField(primary_key=True)
title = models.CharField(db_column='Title', blank=True, null=True, max_length=250)
url = models.CharField(db_column='Url', unique=True, max_length=250, blank=True,
null=True)
created_at = models.DateTimeField(db_column='Created_at')

return AllTables

最佳答案

Q 对象用于进行复杂的逻辑查询。

使用语法:AND (&) 和 OR (|)

from django.db.models import Q


t.objects.filter(Q(id__icontains=q_id) | Q(title__icontains=q_title) | Q(url__icontains=q_url))

关于python - Django 多个搜索词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46429768/

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