django 如何在搜索后将用户重定向到另一个模板?

epggiuax  于 2022-11-26  发布在  Go
关注(0)|答案(1)|浏览(192)

我有一个搜索表单,其中用户搜索的模型显示在搜索表单下面,但我希望它显示在另一个页面。我试图寻找一种方法来重定向用户到另一个网址时,搜索完成,并显示过滤的数据,但我无法做到这一点。
型号:

class Product(models.Model):
    author = models.ForeignKey(User, default=None, on_delete=models.CASCADE)
    title = models.CharField(max_length=120, unique=True)
    date_posted = models.DateTimeField(default=timezone.now)
    ...

查看者:

def FilterView(request):
    qs = Product.objects.all()
    title_contains_query = request.GET.get('title_contains')
    title_or_author_query = request.GET.get('title_or_author')
    ...

    if is_valid_queryparam(title_contains_query):
        qs = qs.filter(title__icontains=title_contains_query)

    elif is_valid_queryparam(title_or_author_query):
        qs = qs.filter(Q(title__icontains=title_or_author_query) | Q(author__username__icontains=title_or_author_query)).distinct()

    ...

    context = {
        'queryset': qs,
        ...
    }

    return render(request, 'main/form.html', context)

模板:

<form method="GET">
            <div>
                <input type="search" name="title_contains" placeholder="Title contains">
                <span><div><i class="fa fa-search"></i></div></span>
            </div>
            <div>
                <input type="search" name="title_or_author" placeholder="Title or author">
                <span>
                    <div><i class="fa fa-search"></i></div>
                </span>
            </div>
            ...
            <button type="submit">Search</button>
        </form>

        <div>
            <ul>
                {% for product in queryset %}
                <li>
                    {{ product.title }}
                    <span>Author: {{ product.author.name }}</span>
                </li>
                <hr />
                {% endfor %}
            </ul>
        </div>
8fq7wneg

8fq7wneg1#

您可以简单地创建两个视图,一个用于显示表单,另一个用于显示查询集,只需正确使用action属性,以便:

**注:**我自己假设的urlpatterns(因为你还没有分享),希望你能理解。

urls.py:

urlpatterns=[
    path("form/", views.form_display, name="form_display"),
    path("some_route/", views.FilterView, name="filter_view")
]

views.py:

def form_display(request):
    return render(request, "main/form.html")

def FilterView(request):
    qs = Product.objects.all()
    title_contains_query = request.GET.get('title_contains')
    title_or_author_query = request.GET.get('title_or_author')
    ...

    if is_valid_queryparam(title_contains_query):
        qs = qs.filter(title__icontains=title_contains_query)

    elif is_valid_queryparam(title_or_author_query):
        qs = qs.filter(Q(title__icontains=title_or_author_query) | Q(author__username__icontains=title_or_author_query)).distinct()

    ...

    context = {
        'queryset': qs,
        ...
    }

    return render(request, 'main/display_query.html', context)

主页/表单.html:

<form method="GET" action="{% url 'filter_view' %}">
     <div>
         <input type="search" name="title_contains" placeholder="Title contains">
         <span><div><i class="fa fa-search"></i></div></span>
     </div>
     <div>
          <input type="search" name="title_or_author" placeholder="Title or author">
          <span> <div><i class="fa fa-search"></i></div> </span>
     </div>
            ...
     <button type="submit">Search</button>
</form>

您可以在这里查看更多信息。

<div>
    <ul>
        {% for product in queryset %}
            <li>
                {{ product.title }}
                <span>Author: {{ product.author.name }}</span>
            </li>
            <hr/>
        {% endfor %}
     </ul>
</div>

**注意:**基于函数的视图是用snake_case而不是PascalCase编写的,这与基于类的视图不同,因此您可以将其从FilterView更改为filter_view

相关问题