2013-04-10 107 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