简体   繁体   English

如何向现有的 url 添加参数?…Django…Paginator

[英]How can i add a param to the existing url?…Django…Paginator

I have made a page with which displays some data from the database.我制作了一个页面,其中显示了数据库中的一些数据。 I have added a paginator to my website.我在我的网站上添加了一个分页器。 my website also has a search bar.我的网站也有一个搜索栏。 so when the user searches for something....the URL is...所以当用户搜索某些东西时...... URL 是......

.../search/?q=xyz

so when I paginate the searched data with...所以当我对搜索到的数据进行分页时......

<a href="?page={{ walls.next_page_number }}"> next page </a>

it makes the URL as...它使 URL 成为...

.../search/?page=2

but that's an error...but, I want something like this...但这是一个错误......但是,我想要这样的东西......

.../search/?q=xyz&page=2

how can I achieve this...我怎样才能做到这一点...

You can use a template tag to dynamically combine the get parameters to your paginator.您可以使用模板标签将获取参数动态组合到分页器。

Here's an example of how I do this;这是我如何执行此操作的示例;

from django import template
from django.utils.http import urlencode

register = template.Library()


@register.simple_tag(takes_context=True)
def url_replace(context, **kwargs):
    query = context['request'].GET.dict()
    query.update(kwargs)
    return urlencode(query)
{% load url_replace %}

            <div class="pagination-container">
                <ul class="pagination">
                    {% if page_obj.has_previous %}
                        <li class="pagination-item">
                            <a href="?{% url_replace page=1 %}">first</a>
                        </li>
                        <li class="pagination-item">
                            <a href="?{% url_replace page=page_obj.previous_page_number %}">previous</a>
                        </li>
                    {% endif %}
                    <li class="pagination-item">
                        <span class="current">
                            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                        </span>
                    </li>

                    {% if page_obj.has_next %}
                        <li class="pagination-item">
                            <a href="?{% url_replace page=page_obj.next_page_number %}">next</a>
                        </li>
                        <li class="pagination-item">
                            <a href="?{% url_replace page= page_obj.paginator.num_pages %}">last</a>
                        </li>
                    {% endif %}
                </ul>
            </div>

There is no way to write a relative URI that preserves the existing query string while adding additional parameters to it.没有办法编写一个相对 URI 来保留现有的查询字符串,同时向它添加额外的参数。

You have to add your parameter to the actual url that your link looks like this:您必须将参数添加到实际的 url 中,您的链接如下所示:

<a href="?q=xyz&page=2"> next page </a>

or something like this:或类似的东西:

<a href="/search?q=xyz&page=2"> next page </a>

I think you get it:)我想你明白了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM