Разбиение на страницы колб не работает с AWS Chalice

#python #flask #aws-lambda #pagination #chalice

Вопрос:

Разбиение на страницы не работает с AWS chalice, есть ли какой-либо другой способ реализации разбиения на страницы?

 from flask import request

@app.route('/hello/{name}')
def hello_name(name):
    page = request.args.get('page', 1, int)
    print(page)
#    # '/hello/james' -> {"hello": "james"}
    return {'hello': name}
 

Ошибка:

 Traceback (most recent call last):
  File "/Users/thirumal/opt/anaconda3/lib/python3.8/site-packages/chalice/app.py", line 1691, in _get_view_function_response
    response = view_function(**function_args)
  File "/Users/thirumal/git/vfc-lambda/app.py", line 39, in hello_name
    page = request.args.get('page', 1, int)
  File "/Users/thirumal/opt/anaconda3/lib/python3.8/site-packages/werkzeug/local.py", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/Users/thirumal/opt/anaconda3/lib/python3.8/site-packages/werkzeug/local.py", line 307, in _get_current_object
    return self.__local()
  File "/Users/thirumal/opt/anaconda3/lib/python3.8/site-packages/flask/globals.py", line 38, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.
 

Ответ №1:

Пример разбиения на страницы:-

 from chalice import Chalice

app = Chalice(app_name='aws-chalice-tutorial')


@app.route('/pagination')
def pagination():
    app.log.debug("you are in Pagination!")
    return {
        'page': app.current_request.query_params.get('page'),
        'limit': app.current_request.query_params.get('limit'),
    }
 

Запрос:

http://localhost:8000/pagination?page=0amp;limit=10

 {'page':'0','limit':'10'}