Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

python - How to add pagination during filtered in django

my url appear like this '''http://127.0.0.1:8000/menu/?[%27tags%27,%20%271%27]&page=2'''

I want to make 'http://127.0.0.1:8000/menu/?tags=2&page=2'

Thank you all..

munu_extras.py

from django import template
from ..models import Menu
register = template.Library()


@register.simple_tag
def my_url(value,field_name,urlencode=None):
    url = "{}={}".format(field_name,value )


    if urlencode:
        querystring = urlencode.split('=')
        filtered_querystring = filter(lambda p: p.split('=')[0]!=field_name, querystring)
        print(filtered_querystring)
        encode_querystring = '='.join(filtered_querystring)
        url = '?{}&{}'.format(querystring,url)

        
    return url 

In My menu.html

        {% if menus.has_previous %}
            <a href="{% my_url 1 'page' request.GET.urlencode %}"><<<</a>
            <a href="{% my_url menus.previous_page_number 'page' request.GET.urlencode %}"><<</a>
        {% endif %}

        {% if menus.has_next %}
            <a href="{% my_url menus.next_page_number 'page' request.GET.urlencode %}">>></a>
            <a href="{% my_url menus.paginator.num_pages 'page' request.GET.urlencode %}">>>></a>
        {% endif %}
question from:https://stackoverflow.com/questions/65880638/how-to-add-pagination-during-filtered-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use request.GET. So when you enter the url http://127.0.0.1:8000/menu/?tags=2&page=2. The part after the ? will be resulted in request.GET. Suppose, if your urls.py, you have a url: path('menu',views.function,name='function') and in views.py you have a function:

def function(request):
    print(request.GET)
    return HttpResponse('success')

In the print statement output, it will be a dictionary with keys tags and page with values 2 and 2 respectively


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...