首页 > 解决方案 > Page Nation 应用于搜索视图,但不显示搜索结果

问题描述

问题:Page Nation应用于搜索视图,但搜索结果不显示

视图的内容如下。

    if(search_option == "content+title"):
        print("search bycontent and title =================")
        page = request.GET.get('page', '1')
        object_list = MyShortCut.objects.filter(Q(author = user)).filter(Q(title__icontains=search_word) | Q(content1__icontains=search_word) | Q(content2__icontains=search_word)).order_by('-category')
        print('object_list(count)  :::::::::: ' , object_list.count() ) 
        paginator = Paginator(object_list, 10)  # 10 per page
        page_obj = paginator.get_page(page)
        print("page_obj ::::::::::::: ", page_obj)
        context = {'object_list': page_obj}

        return render(request, 'wm/MyShortCut_list_for_search.html', {
            "question_list":page_obj
        })

模板代码为:


    <!-- <table class="table table-borderd"> -->

    search result:
    <table border="1" width="100%">
        <tr>
            <td>num</td>
            <td>category</td>
            <td>title 22</td>
            <td>name</td>
        </tr>
        <tbody>
        {% if object_list.exists %}
            {% for p in object_list %}
                <tr>
                    <td>
                        {{ forloop.counter }}
                    </td>
                    <td>
                        <a href="/wm/myshortcut/category/{{p.category}}/">
                            {{p.category}}
                        </a>
                    </td>
                    <td>
                        {{p.title}}
                        <a href="#wm_detail_{{p.id}}" class="badge badge-dark" data-toggle="collapse">detail</a>
                        {% ifequal p.type.type_name "input" %}
                        <br />
                        <input type="text" id="wm_detail_{{p.id}}" class="collapse " value="{{p.content1}}" size="100%" />
                        {% endifequal %}

                        {% ifequal p.type.type_name "textarea" %}
                        <br />
                        <textarea id="wm_detail_{{p.id}}" class="collapse" cols="100" rows="10">{{p.content2}}</textarea>
                        {% endifequal %}

                        {% ifequal p.type.type_name "summer_note" %}
                        <br />
                        <div id="wm_detail_{{p.id}}" class="collapse" width="100%">{{p.content2 | safe}}</div>
                        {% endifequal %}

                    </td>

                    <td>
                        <a href="" onclick="change_user_for_search(this)" id={{p.author}}>
                            {{p.author}}
                        </a>
                    </td>
                </tr>
            {% endfor %}
        {% else %}
            <tr>
                <td colspan="4">
                    <h4>there is no search results</h4>
                </td>
            </tr>
        {% endif %}

        </tbody>
    </table>

    {% for page_number in question_list.paginator.page_range %}
    <!-- ---------------------------------------- [edit] ---------------------------------------- -->
    {% if page_number >= question_list.number|add:-5 and page_number <= question_list.number|add:5 %}
    <!-- ---------------------------------------------------------------------------------------- -->
    {% if page_number == question_list.number %}
    <li class="page-item active" aria-current="page">
        <a class="page-link" href="?page={{ page_number }}">{{ page_number }}</a>
    </li>
    {% else %}
    <li class="page-item">
        <a class="page-link" href="?page={{ page_number }}">{{ page_number }}</a>
    </li>
    {% endif %}
    <!-- ---------------------------------------- [edit] ---------------------------------------- -->
    {% endif %}
    <!-- ---------------------------------------------------------------------------------------- -->
    {% endfor %}

输出是: 在此处输入图像描述

你能知道问题是什么吗?

也许是视图问题或模板问题?

我会很感激你的回复。

谢谢你让我知道如何解决它。

祝你今天过得愉快

标签: django

解决方案


您提供给 html 上下文 -question_list不是object_list 在 html 中您检查 {% if object_list.exists %}

    context = {'object_list': page_obj}

    return render(request, 'wm/MyShortCut_list_for_search.html', {
        "question_list":page_obj
    })

改成

return render(request, 'wm/MyShortCut_list_for_search.html', context)

推荐阅读