Jud*_*123 5 python django url pagination view
我已经在 django web 应用程序中实现了搜索栏和功能,如下所示:
def search_products(request):
search_query = request.GET.get('search', '')
products = Product.objects.filter(Q(name__icontains=search_query) | Q(brand__icontains=search_query))
paginator = Paginator(products, 40)
page_number = request.GET.get('page', 1)
page = paginator.get_page(page_number)
if page.has_next():
next_url = f'?page={page.next_page_number()}'
else:
next_url = ''
if page.has_previous():
prev_url = f'?page={page.previous_page_number()}'
else:
prev_url = ''
return render(request, 'store/search_products.html',
context={'products': page.object_list, 'page': page, 'next_page_url': next_url,
'prev_page_url': prev_url})
Run Code Online (Sandbox Code Playgroud)
URL 设置如下:
urlpatterns = [
path('', views.store_view, name='store'),
path('wishlist/', views.wishlist_view, name='wishlist'),
path('update_item/', views.updateItem, name='update_item'),
path('search_products/', views.search_products, name='search_products'),
]
Run Code Online (Sandbox Code Playgroud)
搜索结果的 HTML 如下:
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item {% if not prev_page_url %} disabled {% endif %}">
<a class="page-link" href="{{ prev_page_url }}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
{% for n in page.paginator.page_range %}
{% if page.number == n %}
<li class="page-item active" aria-current="page">
<a class="page-link" href="?page={{ n }}">{{ n }}
<span class="sr-only"></span>
</a></li>
{% elif n > page.number|add:-4 and n < page.number|add:4 %}
<li class="page-item">
<a class="page-link" href="?page={{ n }}">{{ n }}</a>
</li>
{% endif %}
{% endfor %}
<li class="page-item {% if not next_page_url %} disabled {% endif %}">
<a class="page-link" href="{{ next_page_url }}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
Run Code Online (Sandbox Code Playgroud)
我认为问题可能是由于搜索查询在页码之前输入 URL (?page={{ n }}),但这可能是错误的。我不确定如何解决此问题或实施此更改。任何帮助,将不胜感激。
您的分页不起作用的原因是,当您单击另一个页面链接时,页面查询参数会替换搜索查询参数,并且您的搜索结果将消失。
解决方案是将页面查询参数附加到搜索查询参数,例如,这将是这样的:url/?search=example&page=2
您可以这样做:
在您的应用程序目录中创建一个名为 templatetags 的文件夹
在 templatetags 文件夹中创建一个名为 pagination_tags.py 的文件(名称由您决定)
# Your template tag in app_name/templatetags/pagination_tags.py
from django import template
from urllib.parse import urlencode
register = template.Library()
@register.simple_tag
def url_replace (request, field, value):
dict_ = request.GET.copy()
dict_[field] = value
return dict_.urlencode()
Run Code Online (Sandbox Code Playgroud)
创建模板标签后,将其加载到 html 页面中并使用它
<!-- load your templatetag in your html file -->
<!-- you should load your .py file -->
{% load pagination_tags %}
<!-- now you can use it -->
<!-- Here is an Example to know how to use it -->
<a class="page-link text-dark" href="?{% url_replace request 'page' 1 %}">1</a>
<!-- first param is request , second param is is your lookup field and third param is your page number -->
Run Code Online (Sandbox Code Playgroud)