我的网站搜索无法使用tags_list.html。我认为它与分页设置有关。但我无法确定它。如果我从我的tags_list.html运行搜索。我没有结果。但是,如果我从网站索引中做到这一点就可以了。继承我的代码。
我的代码代码 views.py:
def tag_list(request, slug=None):
instance = get_object_or_404(Tag, slug=slug)
ins = instance.post_set.all()
query = request.GET.get("q")
if query:
ins = ins.filter(
Q(title__icontains=query) |
Q(content__icontains=query) |
Q(user__first_name__icontains=query) |
Q(user__last_name__icontains=query)
).distinct()
paginator = Paginator(ins, 1)
page_request_var = "tags"
page = request.GET.get(page_request_var)
try:
queryset = paginator.page(page)
except PageNotAnInteger:
queryset = paginator.page(1)
except EmptyPage:
queryset = paginator.page(paginator.num_pages)
context = {
"queryset": queryset,
"paginator": paginator,
"page_request_var": page_request_var,
}
return render(request, "posts/tag_list.html", context)
分页:
<div class="text-center" style="margin-bottom: 20px">
<ul class="pagination">
{% if queryset.has_previous %}
<li><a href="?tags=1"><<</a></li>
<li><a href="?{{ page_request_var }}={{ queryset.previous_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">prev</a></li>
{% endif %}
{% for i in paginator.page_range %}
<li {% if page_obj.number == i %} class="active" {% endif %}><a href="?tags={{i}}">{{i}}</a><li>
{% endfor %}
{% if queryset.has_next %}
<li><a href="?{{ page_request_var }}={{ queryset.next_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a></li>
<li><a href="?tags={{ queryset.paginator.num_pages }}">>></a></li>
{% endif %}
</ul>
urls.py
url(r'^tag/(?P<slug>[\w-]+)/$', tag_list, name="tag_index"),
以及它在网址中的显示方式
http://localhost:8000/posts/tag/third-slug/?q=tesla
任何有关如何在语法上纠正这一点的帮助都是受欢迎的。感谢
答案 0 :(得分:0)
我明白了。所有内容我都改为查询集并且有效
def tag_list(request, slug=None):
instance = get_object_or_404(Tag, slug=slug)
ins = instance.post_set.all()
queryset_list = Post.objects.active()
query = request.GET.get("q")
if query:
queryset_list = queryset_list.filter(
Q(title__icontains=query) |
Q(content__icontains=query) |
Q(user__first_name__icontains=query) |
Q(user__last_name__icontains=query)
).distinct()
paginator = Paginator(queryset_list, 1)
page_request_var = "tags"
page = request.GET.get(page_request_var)
try:
queryset = paginator.page(page)
except PageNotAnInteger:
queryset = paginator.page(1)
except EmptyPage:
queryset = paginator.page(paginator.num_pages)
context = {
"queryset": queryset,
"paginator": paginator,
"page_request_var": page_request_var,
}
return render(request, "posts/tag_list.html", context)