我有在 Windows 上编写的 python Flask 应用程序。 我正在尝试使用 pythonanywhere.com 托管来部署它。
我有以下结构:
app.py - my main application
-templates - here I have .html files
-default - here I have img, js, css subfolders.
所以我对 Linux 系统上的文件结构感到困惑。 例如,这是 pythonanywhere.com 上的文件结构
> .bashrc 2015-02-19 17:55 546 bytes
> .gitconfig 2015-02-19 17:55 266 bytes
> .profile 2015-02-19 17:55 79 bytes
> .pythonstartup.py 2015-02-19 17:55 77 bytes
> .vimrc 2015-02-19 17:55 4.4 KB
> README.txt
那么我应该把我的文件放在哪里?有托管吗?
最佳答案
我推荐这样的文件夹结构:
home
└── yourusername
└── myflaskapp
├── app.py
├── default
│ ├── css
│ ├── img
│ └── js
└── templates
└── index.html
在PythonAnywhere中,您可以进入"file"选项卡来创建文件夹并逐一上传文件。如果您有大量文件,您可能需要查看 getting files in and out of PythonAnywhere 的指南
上传文件后,您需要执行以下操作:
- 转到“网络”标签并设置新的网络应用
- 选择“手动配置”
- 编辑 wsgi 文件,并将
/home/yourusername/myflaskapp
添加到 sys.path - 添加一行,例如
from app import app as application
(如果需要,请查看本指南 help with sys.path / import error issues )
点击重新加载,您应该会看到您的网络应用程序。
然后为了让 css 和 javascript 正常工作,您可能需要设置静态文件映射。
- 在“Web”选项卡上,输入新的静态文件条目,
- 使用 URL
/static/
(或者可能是/default/
,这取决于您在模板中放入的内容), - 路径应为
/home/yourusername/myflaskapp/default
。
- 使用 URL
关于python - 部署 Flask 应用程序 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28614626/