Use Django to present data from a database and page it out

Source: Internet
Author: User


Don't laugh this is too simple oh ~ ~ the gods are passing. Please ignore this article automatically ...

To present the data and show it, the results are as follows:

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201108/18/0_1313645999NHt4.gif "width=" 310 "height=" 122 " Style= "Border:none;"/>

1, need to build a project Oh ~

In project, you want to include a presentation of HTML pages and a "background" python page. In order to separate the. html and. py files, create a Templates folder under Project's folder to hold the HTML file. The built project is as follows:

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201108/18/0_1313644161B2N0.gif "width=" "height=" 30 " Style= "border:none;width:152px;height:161px;"/>

2. Open test.py for editing.

1) First, to connect to the database, where the mysqldb need to download the installation itself, it is necessary for Python to connect to the MySQL database ~ Of course ~ If the horizontal gray often high drop children's shoes can write their own. The connection code is as follows ~ information about the database varies depending on your database.

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201108/18/0_1313644692l3YU.gif "width=" 284 "height=" 39 " Style= "border:none;width:401px;height:45px;"/>

2) The next step is to extract the data from the database using SQL statements. We used the limit statement in SQL because we wanted to display the page pagination. At the same time we also need to define a variable curr_page to get the current page is the first few pages. The next thing to do is to assign a results variable to the data loop that was taken out of the database. So as long as the results results to the. html file, the data can be recycled output 650) this.width=650; "Src=" http://hi.csdn. Net/attachment/201108/18/0_1313646809r55u.gif "width=" 508 "height=" 145 "style=" border:none;width:507px;height : 119px; "/>

Add: The above code defines the most of the variables count is the total number of data in the database is removed. A SELECT statement that is nested in a SELECT statement represents the total number of rows of data in the query database and can be listed separately.

3) The next task is to define a number of variables that will be used for pagination

Num_pages: Total pages, last_page: Last, has_previous: Determines whether the current page has a previous page, Has_next: Determines whether the current page has the next page,

Previous_page_number: The previous page of the current page, Next_page_number: The next page of the current page, defined as follows:

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201108/18/0_13136479874hj0.gif "width=" 233 "height=" 208 " Style= "Border:none;"/>

4) The next thing to do is to pass the variables defined in the. py file to the. html file. We use return Render_to_response to complete:

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201108/18/0_1313648281h9i2.gif "width=" 444 "height=" 85 " Style= "Border:none;"/>

5) But be aware that the CUR and conn that we have previously opened need to be closed.

Cur.close ()
Conn.close ()

In this way, our. py file is ready to write.

3. Editing of HTML files

With the above basic ~html file idea is very clear, is to put the variables just submitted according to your favorite location output, to see the code:

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201108/18/0_13136486958lzX.gif "width=" 414 "height=" 405 " Style= "border:none;width:366px;height:355px;"/>

4, now our page can not run ~ because the server can not find our code. The next thing to do is to modify the urls.py and settings.py files.

1) in the urls.py file Urlpatterns = patterns (R ' ^test/$ ', ' hui.test.getdata '), where the specific name depends on the circumstances

2) Add './templates ' to the template_dirs of the settings.py file, which is our custom template folder to store the HTML file

5,, and now start the server, in the Address bar to enter the relevant address can ~ ~ ~ Don't forget ~ to show the first page address in the/?page=1 OH


This article is from the "11876896" blog, please be sure to keep this source http://11886896.blog.51cto.com/11876896/1951208

Use Django to present data from a database and page it out

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.