django应用中的多字段搜索结果错误

问题描述 投票:2回答:1

我想在Django应用中为我的数据库创建一个多字段搜索。仅当用户使用html表单中的all字段时,我的搜索工作才正确。

如果用户不使用某些字段,或者某些字段为空或空白然后我的查询返回所有结果(如apps=movies.objects.all()),这是错误的,因为不需要用户使用所有字段但随时可以在需要搜索的字段中使用。

任何想法如何解决?

这里是我的代码:

models.py:

class category(models.Model):
    title = models.CharField(max_length=100, blank=True, null=True)

class movies(models.Model):
    code = models.CharField(max_length=100, blank=True, null=True)
    code_2 = models.CharField(max_length=100, blank=True, null=True)
    name = models.CharField(max_length=100, blank=True, null=True)
    year = models.CharField(max_length=100, blank=True, null=True)
    movies_title=models.ForeignKey('category', blank=True, null=True)
    link=models.CharField(max_length=100, blank=True, null=True)

html形式:

<form method="POST" action="{%url 'search'  %}">{% csrf_token %}
  <select name="link">
    <option value=""></option>
    <option value="0">link 1</option>
    <option value="1">link 2</option>
    <option value="2">link 3</option>
  </select>
  <select name="category">
    <option value=""></option>
    {% for data in cat%}
    <option value="{{ data.id }}">{{ data.title }}</option>
 {% endfor %}
  </select>
  <select name="year">
    <option value=""></option>
        {% for data in apps%}
    <option value="{{ data.id }}">{{ data.year }}</option>
 {% endfor %}
  </select>
    code: <input type="text" name="code"><br>
    code_2: <input type="text" name="code_2"><br>
    name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

views.py:

def search_erga(request):
    apps=movies.objects.all()
    cat=category.objects.all()
    template='movies.html'

    query_link=request.GET.get('link')
    query_category = request.GET.get('category')
    query_year=request.GET.get('year')
    query_code = request.GET.get('code')
    query_code_2=request.GET.get('code_2')
    query_name = request.GET.get('name')

    if any(
        (
            query_link is not None,
            query_category is not None,
            query_year is not None,
            query_code is not None,
            query_code_2 is not None,
            query_name is not None,
        )
    ):
        apps_1=movies.objects.filter(link__icontains=query_link,
                                   movies_title__icontains=query_category,
                                   year__icontains=query_year,
                                   code__icontains=query_code,
                                   code_2__icontains=query_code_2,
                                   name__icontains=query_name)

        context={
            'apps':apps,
            'cat':cat,
            'apps_1':apps_1
        }
    else:
        context={
            'apps':apps,
            'cat':cat,
        }


    return render(request, template, context)
python html django forms search
1个回答
2
投票

您应该删除以上注释中提到的apps_1-并将其重命名为movies

您想细分if标签。

例如:

if query_link != None or query_category != None:
    filters...

这意味着如果query_link为None而query_category不是,那么您最终将寻找没有查询链接的应用。

您想做的是:

movies = Movie.objects.all()

if query_link:
    movies = movies.filter(link__icontains=query_link)

if query_category:
    movies = movies.filter(movies_title__icontains=query_category)

...

在Python和Django中,您可以依赖简单的条件,例如if query_link:。您不需要if query_link != None。 Python可以满足您的期望。在其他语言中,您将必须检查它是否不为null也不为空字符串。 Python为您做到了。这也适用于Django的许多许多领域。例如,如果user.memberships:足够(无需检查是否设置了变量以及是否至少有一个成员身份)...

© www.soinside.com 2019 - 2024. All rights reserved.