{Oldboy-django] [2 in-depth Django] paging feature

Source: Internet
Author: User
Tags virtual environment

1 Django comes with paging

1.1 Pagination Templates

<! DOCTYPE html>"en">"UTF-8"> <title>Title</title> <link rel="stylesheet"href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">class="Table"> <thead> <tr> <th>id</th> <th>name</t h> <th>age</th> <th>type</th> </tr> </ Thead> <tbody>            {% forRowinchCurrent_page_obj%}                <tr> <td>{{row.id}}</td> <td>{{row.name}}</td>                <td>{{row.age}}</td> <td>{{row.ut.title}}</td> </tr>            {% ENDFOR%}        </tbody> </table> <div>        {%ifCurrent_page_obj.has_previous%}            <a href="/fenye/?page={{Current_page_obj.previous_page_number}}"> Prev </a>        {% ENDIF%}        {% forPageinchCurrent_page_obj.paginator.page_range%}            <a href="/fenye/?page={{Page}}">{{page}}</a>        {% ENDFOR%}        {%ifCurrent_page_obj.has_next%}            <a href="/fenye/?page={{Current_page_obj.next_page_number}}"> Next </a>        {% ENDIF%}    </div></body>View Code

1.2 Paging View functions

def fenye (request): User_list=models. UserInfo.objects.all () # Implementing paging Functionality fromdjango.core.paginator Import page,paginator current_page_number= Request. GET.Get('page') Paginator= Paginator (User_list,Ten) # Print (paginator.count) #数据总行数305# Print (paginator.num_pages) #总页数 to# Print (paginator.object_list) #当前页的数据 Queryset[obj,obj] # print (paginator.per_page) #每一页显示多少行数据10 # Print (paginator.page_range) #页数的范围 (1, +), is a range (1, +), which is an array of # print (Paginator.page (1)) #里面的1表示页码数,Try: Current_page_obj=Paginator.page (Current_page_number) # is also a Queryset object [Obj,obj,..] Except Exception asE:current_page_obj= Paginator.page (1) # Print (Current_page_obj.has_next ()) #当前页是否有下一页 # Print (current_page_obj.next_page_nu Mber ()) #当前页的下一页码 # Print (Current_page_obj.has_previous ()) # Does the current page have a previous page # print (current_page_ob J.previous_page_number () # previous page # print (current_page_obj.object_list) # The current page of the data row list Queryset[obj,obj,.            ..] # print (current_page_obj.number) # current page # print (current_page_obj.paginator) # Pagina Tor ObjectreturnRender (Request,'fenye.html', {'Current_page_obj': Current_page_obj})
View Code

 

2 Customizing paging

2.1 Templates

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Title</title>    <Linkrel= "stylesheet"href= "/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"></Head><Body>    <P>User Information</P>    <Tableclass= "Table">        <thead>            <TR>                <th>Id</th>                <th>Name</th>                <th>Age</th>                <th>Type</th>            </TR>        </thead>        <tbody>{% for row in user_list%}<TR>                    <TD>{{Row.id}}</TD>                    <TD>{{Row.name}}</TD>                    <TD>{{Row.age}}</TD>                    <TD>{{Row.ut.title}}</TD>                </TR>{% endfor%}</tbody>    </Table>{# {{Page_info.pager|safe}}#}<navAria-label= "Page navigation">        <ulclass= "pagination">{{Page_info.pager|safe}}</ul>    </nav>{#    <Div>#} {##} {# {% if current_page_obj.has_previous%}#} {#            <ahref= "/fenye/?page={{Current_page_obj.previous_page_number}}">Previous page</a>#} {# {% endif%}#} {# {% for page in Current_page_obj.paginator.page_range%}#} {#            <ahref= "/fenye/?page={{page}}">{{Page}}</a>#} {# {% endfor%}#} {##} {# {% if Current_page_obj.has_next%}#} {#            <ahref= "/fenye/?page={{Current_page_obj.next_page_number}}">Next page</a>#} {# {% endif%}#} {##} {##} {##} {#    </Div>#}</Body></HTML>
View Code

2.2 Custom Modules

#!/usr/bin/env pythonImportOSImportSYSif __name__=="__main__": Os.environ.setdefault ("Django_settings_module","day4.settings")    Try:         fromDjango.core.managementImportExecute_from_command_lineexceptImporterror:#The above import may fail for some and other reason. Ensure that the        #issue is really, Django is missing to avoid masking other        #exceptions on Python 2.        Try:            ImportDjangoexceptImporterror:RaiseImporterror ("couldn ' t import Django. Is you sure it ' s installed and"                "available on your PYTHONPATH environment variable? Did you"                "forget to activate a virtual environment?"            )        Raiseexecute_from_command_line (SYS.ARGV)
View Code

2.3 views

def custom_fenye (request): fromutils.pagation_define Import PageInfo current_page_number= Request. GET.Get('page') All_count=models. UserInfo.objects.all (). Count () Page_info= PageInfo (Current_page_number, All_count,"/custom_fenye.html/") User_list= Models. UserInfo.objects.all () [Page_info.start ():p age_info.end ()] # list slicing operations A[start:end] Start<= Index <EndreturnRender (Request,'custom_fenye.html', {'user_list': User_list,'Page_info': Page_info})
View Code

{Oldboy-django] [2 in-depth Django] paging feature

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.