【问题标题】:how to add bootstrap, css, images ,js into google app engine?如何将 bootstrap、css、图像、js 添加到谷歌应用引擎中?
【发布时间】:2025-12-29 10:20:12
【问题描述】:

我已经使用 bootstrap 创建了 Web 应用程序。我将它上传到了谷歌应用引擎。我的问题是,它没有显示我添加的任何图像、js 或 css。请帮助我。我的文件夹结构如下。

-myapp
  -css
  -js
  -images
  -fonts
  -index.html
  -otherfiles.html....

我编辑了 app.yaml 如下

application: website-example
version: 1
runtime: python
api_version: 1

default_expiration: "7d"

handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css|swf|xml))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css|swf|xml))

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

- url: /.*
  script: main.p

我编辑了 main.py 如下。

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class IndexHandler(webapp.RequestHandler):
    def get(self):
        if self.request.url.endswith('/'):
            path = '%sindex.html'%self.request.url

        self.redirect(path)

    def post(self):
        self.get()

application = webapp.WSGIApplication([('/.*', IndexHandler)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

请帮帮我。我想不通。

【问题讨论】:

    标签: javascript css twitter-bootstrap google-app-engine python-2.7


    【解决方案1】:

    对于你的情况,我会使用类似的东西:

    application: website-example
    version: 1
    runtime: python
    api_version: 1
    
    default_expiration: "7d"
    
    handlers:
    - url: /css
      static_dir: css
    
    - url: /js
      static_dir: js
    
    - url: /img
      static_dir: images
    
    - /index.html
      static_files: index.html
      upload: index\.html
    
    - url: /.*
      script: main.py
    

    css、js 和图像的静态目录背后的基本思想是,只要请求 url 以这些 url 开头,这些目录下的所有内容都将被静态提供(因此,您可以提供 /img/sub_dir/foo 之类的东西。 png)

    对于静态 html,我使用了单个文件示例,但您也可以将它们放在单独的目录中并从那里静态提供服务

    【讨论】: