In the development of the blog system, we have a need to display the author has published a list of blog posts, or to display the author's followers of the list of articles. It is not complicated to implement this function, just filter out the author's article in the database where the article is stored, and then render the HTML display.
However, this method may be feasible in the case of few articles, and when the number of articles becomes more numerous, it is not possible to display all the articles on one page. At this point, the list of articles is paginated, and each page displays only the specified number of articles.
How should this function be implemented? One of the intuitive ways we can think of is to group the list of articles that are filtered from the database, displaying only one set of articles at a time. The list of articles for the specified group is then displayed according to the user's needs.
Flask-sqlalchemy is about Flask one for database management. In this article we use an example of employee display.
First, we create a sqlalchemy pair like db.
From flask import flask, render_template,requestfrom flask_sqlalchemy Import Sqlalchemyapp = Flask (Name,static_url_path = ') App.debug = Trueapp.secret_key = "FAEFASDFAF" app.config[' sqlalchemy_database_uri '] = ' sqlite:///./db/personal.db ' # app configuration, specify database path app.config[' sqlalchemy_track_modifications ' = trueapp.config[' sqlalchemy_echo '] = True db = SQLAlchemy (APP)
We then use DB to create the employee table:
From datetime import Datetimeclass Employee (db. Model): ' employee ' ' tablename = ' employee ' ID = db. Column (db. Integer, primary_key=true) name = db. Column (db. String ( gender) = db. Column (db. String) job = db. Column (db. String) birthday = db. Column (db. DateTime) Idcard = db. Column (db. String) address = db. Column (db. String) salary = db. Column (db. String) release_time = db. Column (db. DateTime) def init (self, name, gender, job, birthday, Idcard, address, salary, Release_time=none): self.name = Name Self.gender = gender self.job = job self.birthday = Birthday Self.idcard = Idcard self.address = Address self.salary = salary self.release_time = Release_time if release_time else DateTime.Now () def repr (self): return ' < employee {},{},{},{}> '. Format (self.id, Self.name, Self.salary, self.address)
Once the table is created, we can query the data from the table.
From flask import render_templatefrom flask.views import Methodviewclass Employeelistview (Methodview): # Get employee Information def Get (self,page=1): employees = Employee.query.paginate (page,per_page=10) return render_template (' Employeelist.html ', employees=employees)
Above we through the query, the staff information, and then passed to the front desk a template. (We used the Flask blueprint for the sub-module, assuming that we define the above as a view function: Employee.list)
Note: Paginate is a pagination method, the first parameter is the page number, and the second is how many bars are displayed per page. However, the resulting result is not a list, you need to upload to the foreground value plus a. Items, as illustrated below. (using JINJA2 templates)
{% for item in employees.items%}
As shown above, when using JINJA2 to fetch values, you need to add. Items after the value passed in the background.
Continue with the above page, here we will again use the JINJA2 template to define a method to achieve the function of paging, this page name is called: helper.html.
{% Macro render_page_data (page_data,page_function)%} <p class= "Text-center" > <ul class= "Page_data" > <li><a href= "{{url_for (Page_function,page = 1)}" > Home </a></li> {% if Page_data.has_prev%} <li><a href= "{{url_for (page_function,page = page_data.prev_num)}}" >«</a></li> {% Endi F%} {% for page in page_data.iter_pages ()%} {% if page%} {% if page!=page_data.page%} <li><a href= "{{url_for (page_function,page = page)}}" >{{page}}</a></li> {% Else%} <li class= "Active" ><a href= "#" >{{page}}</a></li> {% endif%} {% ENDIF%} {% endfor%} {% if Page_data.has_next%} <li><a href= "{{url_for (Page_function,page = Page_dat A.next_num)}} ">»</a></li> {% endif%} <li><a href=" {{url_for (page_function,page = page _data.pages)}} "> Last ≪/a></li> </ul> </p>{% endmacro%}
The above is the use of JINJA2 syntax to define a distribution method, this method, we passed two parameters, the first is the background to pass through the DB paging query out of the data. The second is the way we query the data.
In particular, there are several important ways in which paging data can be explained:
Has_next: Returns True if there is at least one page after the current page
Has_prev: Returns True if there is at least one page before the current page
Next_num: Number of pages on next page
Prev_num: Number of pages on previous page
After working through the above, it was only one step behind, and we needed to import the method we just defined in our template page via Jinja2, that is, in our employeelist.html above.
{% import ' helper.html ' as helper%}
After the import, we can call it where we need it.
{{helper.render_pagination (employees, ' Employee.list ')}}
The above is the method we defined before calling us. The first argument, the value we pass from the background, and the second one is the view function in the background.
After doing the above, we are done, look below, we have achieved.