我的 Django 应用程序搜索功能无法从标签页面运行
My Django apps search function wont work from the tags page
我的站点搜索在 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"),
以及它在 url
中的外观
http://localhost:8000/posts/tag/third-slug/?q=tesla
欢迎提供有关如何在语法上更正此问题的任何帮助。谢谢
我明白了。我将 ins 中的所有内容更改为查询集并且有效
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)
我的站点搜索在 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"),
以及它在 url
中的外观http://localhost:8000/posts/tag/third-slug/?q=tesla
欢迎提供有关如何在语法上更正此问题的任何帮助。谢谢
我明白了。我将 ins 中的所有内容更改为查询集并且有效
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)