【发布时间】:2022-01-04 10:23:13
【问题描述】:
这是一个 URL 缩短应用程序。应用程序结构如下: App structure
在 forms.py 中,我有自定义验证器:validate_url() 和 validate_short_url()
使用APP_URL; APP_URL = "localhost:5000/"
我可以在本地运行,但有很多情况下应用程序域可以更改:
- 通过 docker 镜像运行;
- 托管(例如在 Heroku 上);
- 更改端口值;
所以每次我以不同的方式运行这个烧瓶应用程序时,我都必须更改 APP_URL 的值,这不是最佳做法
总而言之,我想使用flask.Request.url_root之类的东西来避免一次又一次的手动编写
当我尝试使用 flask.request 时,我得到以下回溯:
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.
forms.py 已发布here
该应用程序已经托管在 Heroku 上,这里是链接:https://qysqa.herokuapp.com/
【问题讨论】:
-
为什么 url 缩短功能将依赖于 APP_URL?
-
短 URL 基本上包含其中的 APP_URL。它是 f"{APP_URL}/{token}",因此如果用户将其粘贴到浏览器中,他们会被重定向到原始 URL。我已经在 heroku 上托管了我的应用程序,您给了我一个想法,让我留下一个链接以了解其特定用途。
-
实际上,我可以继续使用 TokenForm,但我仍然需要 APP_URL,特别是它的长度,来设置 Length() 中的 min-max 参数
标签: python flask flask-wtforms