python - Flask 分页不适用于 AWS 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.
解决方案
分页示例:-
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=0&limit=10
{'page':'0','limit':'10'}
推荐阅读
- java - 当用户输入“结束”时如何结束这个程序?
- c# - 使用 StreamWriter 的文件中的 NULL 符号
- css - css 背景颜色属性
- asp.net-core-webapi - 如何手动生成使用公钥签名的 OIDC 令牌?
- log4j - 在 JSON 布局中使用自定义参数 [Log4j 2]
- python - 模拟 django 测试 - 为什么这个补丁函数仍然被调用?
- git - 如何在不下载文件的情况下使用 git clone 设置 repo?
- django - Django 外连接
- javascript - 如果我输入错误的路线,如何在 Node.js 中显示“404 not found”?
- authentication - 删除 ibus 后 Manjaro 无法登录:出现新的登录屏幕并且没有会话可供选择