【问题标题】:how to display django search result in new page如何在新页面中显示 django 搜索结果
【发布时间】:2022-01-18 18:20:08
【问题描述】:

我想在新的 html 页面而不是搜索栏所在的页面上显示我的 django 搜索结果。我已经尝试操作我的表单标签,但当我搜索时它仍然不会重定向到页面,而是停留在同一页面上。

index.html

<form action="{% url 'elements:all-elements' %}" method="get">
  <input type="text" class="form-control" name="q" value="{{ request.GET.q }}" type="text" placeholder="Search Free Web Elements and Resources">
  <button type="submit" class="btn bt-round" ><b><i class="bi bi-search"></i></b></button>
</form>

views.py - 这是处理搜索的视图,我不知道是否会在此处附加任何内容

# Search Function
    query = request.GET.get("q")
    if query:
        vectors = vectors.filter(
            Q(title__icontains=query)).distinct()

【问题讨论】:

    标签: python django forms searchbar


    【解决方案1】:

    您可以为搜索结果呈现新模板。

    
    def search(request):
        query = request.GET.get("q")
        vectors = Model.objects.all()
        if query:
           vectors = vectors.filter(
                Q(title__icontains=query)).distinct()
        context = {"vectors":vectors}
        return render(request,  "template",  context)

    【讨论】:

    • 感谢您的回复,但我希望搜索栏出现在我的主页中。我希望你明白。然后,如果我搜索它会带我到一个新页面来显示结果
    • 这就是为什么您应该使用搜索功能渲染到新模板。
    • 好的,谢谢,现在可以使用了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2021-11-01
    • 2015-11-26
    • 2017-06-21
    • 2011-08-25
    • 2020-11-30
    相关资源
    最近更新 更多