Ask questions about how to page the web implemented by flask

Asked 2 years ago, Updated 2 years ago, 97 views

@app.route('/main', methods=['GET'])
def test():
    test_list = []
    test_items = db.session.query(table_class_name).filter(table_class_name.column == 150 ).limit(9).all()
    for item in test_items:
       test_dict = {}
       test_dict['name1'] = test_items.name
       test_dict['id'] = test_items.id
       test_list.append(test_dict)
   dump_data = json.dumps(test_list)
    return render_template('test.html', dump_data=dump_data)


@app.route('/pagination', methods=['GET'])
def test_paging():
    limit_num =  request.args.get('num')
    test_list = []
   test_items = db.session.query(table_class_name).filter(table_class_name.column == 150 ).limit(9).all() # You have to put a limit range on this part!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    for item in test_items:
       test_dict = {}
       test_dict['name1'] = test_items.name
       test_dict['id'] = test_items.id
       test_list.append(test_dict)
   dump_data = json.dumps(test_list)
    return dump_data

First of all, please consider that the above functions are examples and read the questions
Currently, the final feature you're trying to create is a page on the web implemented by flask, where some information is listed sequentially, and its initial number of data is 9.
In order to see additional data, I want to make the data visible by 5 when the read more button is pressed
For the above code, when you press read more on the page '/main', the limit section is applied at the place '/pagination' so that you can get 5 more after 9 pieces of data on the web.
So in the HTML document, I wrote it in the form of handing over the value to Ajax in '/pagination' How can I fill out the additional data so that every time I press read more, I get more than 5 pieces of data?
First of all, when I press read more, num is given 5 to pass the number 5 in the form of json When you receive the number 5 from '/paginatnion' and click limit 11-15 for the first time, data is added to 5 that was first passed, and the 5 is added to 10 againI was going to make a crab I don't know how to add the number 5 every time I press read more and how to use the limit function grammar in the part using sqlalchemy I would appreciate it if you could also tell me how to use sqlalchemy limit
Regarding the question, even if I google it, I don't know the access itself, so I can't get the necessary information.

sqlalchemy python flask web pagination

2022-09-21 20:57

1 Answers

I think it's roughly this way.

1. When /pagination responds to the data, JS stores the eigenvalue of the last of the data answered somewhere.

2. The next time you request /pagination, find the stored eigenvalue and put the value in the request.

3. Then, the source processing the /pagination request receives the eigenvalue contained in the request, and when inquiring with SQL, it is limited to the eigenvalue after the eigenvalue, and the data is extracted and responded.

# last_created_at is the value contained in the request.
# It is important that both filter() and order_by() are based on the same created_at
next_data = db.session.query(Table).filter(Table.created_at < last_created_at).limit(5).order_by(Table.created_at)

Back to 4.1.

It's not such a difficult concept. Give that a try.


2022-09-21 20:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.