3

我想重寫谷歌Appengine上的靜態網站的網址。 我只想要 http://www.abc.com/about對於http://www.abc.com/about.html 我不需要重寫像abc.com/page?=1之類的東西或任何東西。 我只想弄清楚如何明確重寫html頁面的URL。谷歌appengine的基本html映射或網址重寫(python)

我目前使用(沒有工作)的代碼 -

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

class MainHandler(webapp.RequestHandler): 
    def get(self): 
     template_values = {} 

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


class PostHandler(webapp.RequestHandler): 
    def get(self, slug): 
     template_values = {} 
     post_list = { 
      'home' : 'index.html', 
      'portfolio' : 'portfolio.html',    
      'contact' : 'contact.html', 
      'about' : 'about.html' 
     } 

     if slug in post_list: 
      self.response.out.write('Slugs not handled by C-Dan yet') 
     else: 
      self.response.out.write('no post with this slug') 

def main(): 
    application = webapp.WSGIApplication([('/', MainHandler),('/(.*)', PostHandler)], debug=True) 
    util.run_wsgi_app(application) 

if __name__ == '__main__': 
    main() 
+0

不工作怎麼樣?會發生什麼,你期望會發生什麼? – 2012-04-24 03:30:56

回答

4

爲了您的構造函數,你想:

def main(): 
    application = webapp.WSGIApplication([ 
    ('/', MainHandler), 
    ('/portfolio/', Portfolio), 
    ('/contact/', Contant), 
    ('/about/', About) 
    ]) 
    util.run_wsgi_app(application) 

這意味着任何時候有人去http://www.abc.com/about/,他們會被'路由'到你的About處理程序。

然後,你必須做一個關於處理程序。

class About(webapp.RequestHandler): 
    def get(self): 
    self.response.out.write(template.render('about.html', None)) 

我不熟悉你的編碼風格,但是我已經證明你在我的所有項目中都爲我工作過。

+1

不要將應用程序定義放在主體中 - 放在它外面。這樣,它只定義一次,並且您可以輕鬆地將您的應用程序轉換爲Python 2.7運行時。另外,您的第一個片段中的縮進是錯誤的。 – 2012-04-24 03:30:43

+0

你會如何將應用程序定義放在主體之外? 修復了縮進問題,謝謝! – mrmo123 2012-04-24 04:56:52

+0

非常感謝mrmo123,工作就像一個魅力! @nick,我使用2.7,如何將應用程序定義放在主體之外?謝謝 – 2012-04-24 13:17:15