使用多个查询的Django筛选

2024-09-30 22:20:03 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的主页上,我有一个搜索栏,当你们搜索某个东西时,它会将你们重定向到一个带有结果(标题和文档类型)的页面。在页面的左侧,我想实现一个按文档类型的过滤器

搜索后,我的url如下所示:http://127.0.0.1:8000/search/?q=something

应用筛选器后:http://127.0.0.1:8000/search/?document_type=Tehnical+report

我不知道如何实现过滤器,只在由搜索页面上的查询(q)过滤的对象列表中进行搜索。另外,我不确定在应用过滤器后,url是应该像这样:http://127.0.0.1:8000/search/?q=something&document_type=Tehnical+report还是像这样http://127.0.0.1:8000/search/?document_type=Tehnical+report

型号.py

DOCUMENT_TYPES = [
    ('Tehnical report','Tehnical report'),
    ('Bachelor thesis','Bachelor thesis'),
    ...
]

class Form_Data(models.Model):
    title           = models.CharField(unique=True, max_length=100, blank=False)
    author          = models.CharField(max_length=100)
    document_type   = models.CharField(choices=DOCUMENT_TYPES, max_length=255, blank=False, default=None)

视图.py

def search_list(request):
    object_list = Form_Data.objects.none()
    document_types = DOCUMENT_TYPES

    query = request.GET.get('q')
    query_list = re.split("\s|(?<!\d)[,.](?!\d)", query)
    document_type_query = request.GET.get('document_type')

    for item in query_list:
        object_list |= Form_Data.objects.filter( Q(title__icontains=item) | Q(author__icontains=item))

    return render(request, "Home_Page/search_results.html")

主页.html

<div class="Search">
    <form action="{% url 'home_page:search_results' %}" method="get">
        <input id="Search_Bar" type="text" name="q">
        <button id="Button_Search" type="submit"></button>
    </form>
</div>

搜索结果.html

{% for form_data in object_list %}
    <h5>{{ form_data.title }}</h5>
    <h5>{{ form_data.document_type }}</h5>
{% endfor %}

<form method="GET" action=".">
    <select class="form-control" name="document_type">
        {% for tag, label in document_types %}
            <option value="{{ tag }}">{{ tag }}</option>
        {% endfor %}
    </select>
</form>

Tags: reportformhttpurl过滤器searchmodelsrequest
2条回答

在我看来,你做得不对。。。我的意思是我不明白你为什么要循环你的query进行过滤。据我所知,你的每一封信都在循环

我当时正在做,我会这样做(用我自己的例子):

<form action='{% url 'products:search' %}' method='get'>
    <input type='text' name='q' id='search' value='' >
    <select name='category' id='category'>
        <option value='' selected ></option>
        <option value='packet'>Packet</option>
        <option value='food'>Food</option>
        <option value='vegetable'>Vegetable</option>
    </select>
    <input type='button' value='submit' >
</form>

views.py:

def search(request):
    products = None
    query = request.GET.get('q')
    category = request.GET.get('category')
    
    if query:
        products = Product.objects.filter(
            Q(name__icontains=query)|
            Q(brand__icontains=query)
        )
    if category:
        # since it is a choice field in the model
        products |= Products.objects.filter(category=category) 

    context = {
        'products': products,
    }
    return render(request, 'products/search_products.html', context)

在这种情况下,如果我按下submit按钮,我会得到如下url:

http://localhost:8000/products/search/?q=something&category=food

有了这些数据,我可以按名称或任何其他我想要的字段过滤产品

我没有看到有人会输入他们的查询,搜索结果中会有输入字段中输入任何字母的所有产品

这将是模型筛选:

query = request.GET.get('q')
document_type_query = request.GET.get('document_type')

object_list = FormData.objects.none()

for item in query.split():
    item_qs = FormData.objects.filter(Q(title__icontains=item) | Q(author__icontains=item))
    if document_type_query:
         item_qs = item_qs.filter(document_type=document_type_query)
    object_list |= item_qs

return render(request, "Home_Page/search_results.html", {"object_list": object_list})

这是网址:

http://127.0.0.1:8000/search/?q=something%20with%20spaces&document_type=Tehnical+report

相关问题 更多 >