Example of Django method to implement fast paging, and django paging instance

Source: Internet
Author: User

Example of Django method to implement fast paging, and django paging instance

Preface

This article mainly introduces Django's quick paging content for your reference and learning. I will not talk much about it below. Let's take a look at the detailed introduction.

Paging

In web development, it is a common requirement to display a large number of products by page. django directly provides ready-made functions for paging, making our development more fast and convenient...

Animation _ Django quick Paging

Sample Code:

In the backend (view function)

From django. shortcuts import renderfrom. models import ShowMyComputer # import method from django. core. paginator import Paginator # Create your views here. def show (request, page_id): # obtain the set of objects to be paged all_goods = ShowMyComputer. objects. all () # create a paging object paginator = Paginator (all_goods, 3) # determine the returned data current_page = paginator based on the current page number. page (page_id) # Make sure that the "page number" obtained by the front-end is an integer page_id = int (page_id) return render (request, 'computer/list.html ', locals ())

In front-end (html template)

<Body >{# display data on the current page #}{% for goods in current_page %}< div class = "my_goods"> <div class = "goods_image">! [Image placeholder] (/static/{goods. goods_image }}) </div> <br> <div class = "goods_name" >{{ goods. goods_name }}</div> </div> {% endfor %} <div class = "page_num" >{# determine whether the 'preput' exists, if yes, the 'previous page' label is retained. Otherwise, the 'previous page' tag # }{% if current_page.has_previous %} <a href = "{% url' computer: show 'current_page.previus_page_number %} "rel =" external nofollow "> previous page </a >{% endif %}{# determine the number of pages #}{ % for index in paginator. page_range % }{# if the page number matches the current page, add a red background #}{% if page_id = index %} <a href = "{% url 'computer: show 'index %} "style =" background-color: red ">{{ index }}</a >{# if the page does not match the current page, displayed normally # }{% else % }< a href = "{% url 'computer: show 'index %} "rel =" external nofollow ">{{ index }}</a >{% endif %}{% endfor %}{# determine whether 'Next' exists, if yes, the 'next page' label is retained. Otherwise, the 'next page' tag # }{% if current_page.has_next %} <a href = "{% url' computer: show 'current_page.next_page_number %} "rel =" external nofollow "> next page </a >{% endif %}</div> </body>

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.