2013-04-10 105 views
1

我正在Python应用程序在谷歌App Engine中的webapp2框架。我一直使用Jinja2作为我的模板引擎和Twitter Bootstrap进行样式设计。在构建了一个漂亮的“layout.html”并让所有其他模板从“layout.html”继承之后,我部署了它。所有页面都会呈现属性,除了一个网址是动态网址的网页。抓住所有网站抓住所有重新渲染css

这里是WSGI处理程序是什么样子:

webapp2.WSGIApplication = ([('/login', LoginPage), 
          ('/updates', Update), 
          ('/updates/.*', Individual)], 
          debug = True) 

# as you can see I'm using the catchall regex for Individual 

在功能上,每个动态生成的URL通过个人妥善处理工作。这里是处理程序,同样,处理程序中的所有内容都正在执行。

class Individual(Handler): 
    def get(self): 
      url_u = str(self.request.url) 
      posit = url_u.find('updates') 
      bus1 = url_u[posit+8:] 
      bus = bus1.replace('%20', chr(32)) 
      b = BusUpdates.all() 
      this_bus = b.order('-timestamp').filter('bus = ', bus).fetch(limit=10) 
      name = users.get_current_user() 
      user = None 
      if name: 
        user = name.nickname() 
      logout = users.create_logout_url(self.request.uri) 

      self.render("individual.html", bus=bus, user=user, this_bus=this_bus, logout=logout) 

一个典型的URL看起来像: http://www.megabusfinder.appspot.com/updates/St%20Louis,%20MO-Chicago,%20IL-4-12-930AM-310PM

这里是我的app.yaml文件

application: megabusfinder 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: no 


handlers: 
- url: /favicon\.ico 
    static_files: favicon.ico 
    upload: favicon\.ico 


- url: /static/stylesheets 
    static_dir: static/stylesheets 


- url: /twitter-bootstrap-37d0a30 
    static_dir: twitter-bootstrap-37d0a30 


- url: /static/xml 
    static_dir: static/xml 

- url: .* 
    script: main.app 


builtins: 
- remote_api: on 


libraries: 
- name: webapp2 
    version: "2.5.1" 
- name: jinja2 
    version: latest 

现在,我以前有 “individual.html” 从我的“布局继承html的”。大约一个小时前,我不再那样做了,我已经手动将“layout.html”中使用的所有必要的引导程序添加到“individual.html”中。即使如此,没有任何造型效果。

在此先感谢。

+0

你是否检查过你可以手动下载css? 如果不检查你的app.yaml。 – 2013-04-10 02:01:15

回答

2

问题是您使用的是样式表的相对URL路径,而不是绝对路径。你这样做:

<link href="styles/bootstrap.css" rel="stylesheet"> 

当你应该这样做:

<link href="/styles/bootstrap.css" rel="stylesheet"> 

的问题是,浏览器会做出这样一个由现有的URL与结合相对URL的请求您的href(或JavaScript文件的src)中提供的相对URL。

在您的根目录页面上,浏览器请求megabusfinder.appspot.com/styles/bootstrap.css。在你的非根页面上,它请求megabusfinder.appspot.com/some/sub/path + styles/bootstrap.css ...它不存在,导致一个404(和一个无风格的页面)。

提供业界领先的斜杠确保浏览器将href路径替换当前路径,而不是相结合的路径。有关如何合并URI的更多信息,请参阅RFC 3986

+1

这是总是让我很小的语法错误。我非常欣赏这个回应。这工作。你还教给我更多关于相对和绝对路径的信息,我知道这很重要,但没有读到太多内容。再次感谢。 – madman2890 2013-04-10 02:16:22