2012-06-10 35 views
1

我是GAE的新手初學者。我現在在GAE上主持一個網站。 我想將http://abc.com/about.html的URL更改爲http://abc.com/about/ 我該怎麼辦?謝謝。如何在GAE中設置動態網址?

這裏是我的main.py:

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

class MainHandler(webapp2.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 About(webapp2.RequestHandler): 
    def get(self): 
     self.response.our.write(template.render('about.html',None)) 

def main() 
    application = webapp2.WSGIApplication([(
     '.', MainHandler), 
     ('about', About), 
     ]) 
    util.run_wsgi_app(application) 

app = webapp2.WSGIApplication([('/', MainHandler)], 
           debug=True) 

這裏是我的app.yaml:

application: nienyiho-test 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: yes 

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

- url: .* 
    script: main.app 

- url: /about 
    static_files: about.html 
    upload: about.html 

libraries: 
- name: webapp2 
    version: "2.5.1" 
+0

爲什麼你不顯示使用你的'app.yaml'文件和實現你的web處理程序的代碼。 –

+0

嗨,亞當,我已經上傳了我的main.py。謝謝。 – lavitanien

回答

2

你需要改變你的路線。您沒有向我們提供創建您的路由的代碼,但是如果您基本上提供靜態HTML文件,那麼@AdamCrossland評論指出您可以使用app.yaml文件來完成此操作。

你的app.yaml文件應該是這個樣子:

application: your_app 
version: 1 
runtime: python27 
api_version: 1 
default_expiration: "1d" 
threadsafe: True 

- url: /about.html 
    static_files: static/html/about.html 
    upload: static/html/about.html 
    secure: never 

- url: /about 
    script: main.app 

- url: /.* 
    script: main.app 

您還可以使用正則表達式作爲@NickJohnson建議here如果你願意,你可以刪除安全線,但我使用https在我的一些應用程序並使用該行來強制哪些路由是安全的而不是。

main.py

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

class MainHandler(webapp2.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 AboutHandler(webapp2.RequestHandler): 
    def get(self): 
     self.response.our.write(template.render('about.html',None) 

# Setup the Application & Routes 
app = webapp2.WSGIApplication([ 
    webapp2.Route(r'/', MainHandler), 
    webapp2.Route(r'/about', AboutHandler) 
], debug=True) 

編輯:20120610 - 增加main.py和更新的app.yaml,以顯示如何路由所產生的內容。

+0

謝謝馬克。我已經上傳了我的app.yaml – lavitanien

+0

但它仍然不起作用.... – lavitanien

+0

請確保URL:/ about在yaml上面的URL:。* – lecstor

相關問題