2013-01-06 127 views
2

我按照教程爲我的站點創建了漂亮的URL,我打算在Google App Engine(Python)上託管它。URL Rewrite - Google App Engine(Python)

問題是它不會顯示subdictories內的索引頁。

我有一個名爲abc.html其在該地址加載罰款 的http:// www.testsite.com/abc

但是我對子目錄索引文件(XYZ和123),其中獲得「T加載到內容區域(在內容區域的空白間隔)

子目錄XYZ將以index.html: http://www.testsite.com/xyz

子目錄123將以index.html內部目錄XYZ: http://www.testsite.com/xyz/123

這裏是我使用的代碼

的app.yaml

application: testsite 
version: 1 
runtime: python 
api_version: 1 
threadsafe: yes 

default_expiration: "1d" 

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

- url: /.* 
    script: main.py 

main.py

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

class MainPage(webapp.RequestHandler): 
    def get(self, p): 
     if p: 
     page = p + ".html" 
     else: 
     p = "main" 
     page = p + ".html" 

      if not os.path.exists(page): 
     page = "404.html" 

     template_values = { 
      "page" : page, 
       p: "first", 
     }; 

     path = os.path.join(os.path.dirname(__file__), 'index.html') 
     self.response.out.write(template.render(path, template_values)) 

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

def main(): 
    run_wsgi_app(application) 

if __name__ == "__main__": 
    main() 

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <title>TestTitle</title> 
    <link href="/static/style.css" rel="stylesheet" type="text/css" media="screen" /> 
</head> 
<body> 
    <div id="header"> 
      <ul> 
       <li><a href="main">Home</a></li> 
       <li><a href="abc">abc</a></li> 
       <li><a href="xyz/123">xyz</a></li> 
      </ul> 
    </div> 

    <div id="content"> 
     <!-- this is where content will be loaded --> 

     {% if page %} 
     {% include page %} 
     {% else %} 
     {% include "main.html" %} 
     {% endif %} 

    </div> 

    <div id="sidebar"> 
     TestSideBar 
    </div> 

    <div id="footer"> 
     TestFooter 
    </div> 
</body> 
</html> 

PS:我遵循的教程是一個動態頁面+ URL重寫指南。動態頁面方面並不是非常必要的。我只是找不到能獲得漂亮URL的教程。

回答

0

首先你的app.yaml或你的main.py是錯誤的。 由於Python27是更現代的運行,我認爲這是app.yaml中 我建議以下的app.yaml:

application: testsite 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: yes 

handlers: 
- url: .* 
    script: main.application 

那你說說名爲index.html內部子目錄中的文件,但他們無處訪問。相反,您訪問'xyz.html'和'xyz/123.html'。

試試下面的代碼剪斷,而不是:

 if p: 
      page = os.path.join(p, "index.html") 
     else: 
      p = "main" 
      page = "main.html" 

順便說一句:你應該考慮模板繼承,而不是包括標籤!