django - 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 %}
你能知道问题是什么吗?
也许是视图问题或模板问题?
我会很感激你的回复。
谢谢你让我知道如何解决它。
祝你今天过得愉快
解决方案
您提供给 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)
推荐阅读
- javascript - React API fetch 返回一个空对象
- javascript - 根据点击的标签显示动态内容
- java - 找不到包导致编译失败
- entity-framework-core - 调用一个存储过程返回一个没有 DbSet 的数据集
- android - 无法使用在 url 中传递的源重定向 web 视图中的用户
- php - PHP过滤器数组在特定日期(RSS提要)之后/期间
- php - 无法使用 php 获取客户端机器的 MAC 地址
- javascript - 将对象数组提取到数组中
- flutter - 如何在颤动中更改国家选择器的宽度?
- arrays - 使用 google sheet 重复文本 + n 次