在Google App Engine中提供静态HTML文件

6 投票
4 回答
6989 浏览
提问于 2025-04-16 15:54

我在为我的Python应用加载静态的 .html 页面时遇到了麻烦。当我点击像 index.html 这样的链接时,页面是空白的,并且在服务器日志中显示404错误。其他静态的 .html 文件,比如 about.html 也是这样。

这个应用程序除了静态文件之外都能正常工作。我尝试过很多地方,但就是无法加载 .html 页面。例如:

INFO 2011-04-16 17:26:33,655 dev_appserver.py:3317] "GET / terms.html HTTP/1.1" 404 -

yaml:

application: quote
version: 1
runtime: python
api_version: 1

handlers:

- url: /index\.html
script: index.py

- url: /
script: index.py

- url: /(.*\.(html))
static_files: static/\1
upload: static/HTML/(.*\.(html))


- url: /favicon.ico
static_files: static/images/favicon.ico
upload: images/favicon.ico
mime_type: image/x-icon

- url: /css
static_dir: static/css

- url: /images
static_dir: static/images

- url: /js
static_dir: static/js

我的静态文件放在 static/HTML 文件夹里,而 index.html 在主文件夹中。

我也试过这样做,但似乎没有任何变化:

- url: /favicon.ico
  static_files: static/images/favicon.ico
  upload: images/favicon.ico
  mime_type: image/x-icon

- url: /css
  static_dir: static/css

- url: /images
  static_dir: static/images

 - url: /js
  static_dir: static/js

 - url: /(.*\.(html))
  static_files: static/\1
  upload: static/HTML/(.*\.(html))

 - url: /index\.html
  script: index.py

 - url: /
 script: index.py

4 个回答

1

你不需要在你的yaml文件中单独定义每一个目录。

handlers:
- url: /static
  static_dir: my_application/static

然后在你用Django渲染的相关html文件中,你可以这样调用你的静态内容,比如:

<script src="/static/less_lib.min.js"></script>
2

在你的静态文件路径中放一个 /HTML

- url: /(.*\.(html))
  static_files: static/HTML/\1
  upload: static/HTML/(.*\.(html))
4

把你的处理脚本放在静态目录处理部分的下面。换句话说,就是把这个移动到最后面。

- url: /index\.html
script: index.py

- url: /
script: index.py

撰写回答