0

我試圖將不同的網址映射到不同的python腳本。gae&python - 在yaml中設置不同的網址:404找不到

這是我的YAML

application: myApp 
version: 99 
runtime: python27 
api_version: 1 
threadsafe: yes 

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

- url: /deleteCustomers 
    script: test.app 

- url: /.* 
    script: main.app 

libraries: 
- name: webapp2 
    version: "2.5.2" 

builtins: 
- remote_api: on 

如果我去http://myapp.appspot.com/test,它說: 「404未找到」 ...... 如果我去http://myapp.appspot.com,合適的劇本啓動(main.app)

這裏是我有同樣的問題 - >HERE 但所提供的解決方案是不爲我工作(即使是相同的代碼!)

這裏的處理程序(測試「2路YAML」,我重複了main.app,持有客戶和商店類加上mainhandler,將其重命名爲test.app。因此,無論main.app和test.app是相同的)

class MainHandler(webapp2.RequestHandler): 
    def get(self): 
     customers = Customers.all() 
     stores = Stores.all() 

     countCustomers= 0 
     countStores= 0 

     for p in customers: 
      p.delete() 
      countCustomers+= 1 
     for p in stores: 
      p.delete() 
      countStores+= 1 

     self.response.out.write("\nDeleted Customers: " + str(countCustomers)) 
     self.response.out.write("\nDeleted Stores: " + str(countStores)) 

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

我想達成什麼是顧客和商店刪除拆分成兩個單獨的呼叫:

http://www.myapp.appspot.com/deleteCustomershttp://www.myapp.appspot.com/deleteStores

感謝提前任何幫助,最好的問候

+0

試試這個:url:/test.*你也可以發佈處理程序和路由到測試處理器的路由器。 – Tkingovr 2013-03-12 16:42:07

+0

它不起作用:| – BeNdErR 2013-03-12 16:45:03

+2

你可以在測試應用中發佈代碼嗎? – Tkingovr 2013-03-12 16:46:31

回答

1

如果你說這兩個腳本是完全相同的然後我假設你使用相同的'/'指向你的MainHandler。我不太確定我是否正確理解你,但這是我的努力來幫助你。爲了達到分裂商店刪除,刪除客戶到2個不同的腳本,你必須將代碼分成映射到每個URL像2個不同的處理程序:

class StoreDeletionHandler(webapp2.RequestHandler): 
def get(self): 
    stores = Stores.all() 

    countStores= 0 

    for p in stores: 
     p.delete() 
     countStores+= 1 

    self.response.out.write("\nDeleted Stores: " + str(countStores))   


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

上面會在路由你main.py腳本與在YAML腳本以下調用:

- url: /.* 
    script: main.app 

,然後在不同的腳本test.py第二url在這種情況下:

class CustomerDeletionHandler(webapp2.RequestHandler): 
    def get(self): 

     customers = Customers.all() 
     countCustomers= 0 

     for p in customers: 
      p.delete() 
      countcustomers+= 1 

     self.response.out.write("\nDeleted Customers: " + str(countCustomers)) 

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

在你的YAML文件中,可以通過網址映射到腳本如下:

- url: /deleteCustomers 
    script: test.app 

還注意到,爲了使所有後續路線被引導到test.py腳本的URL必須先從'/ deleteCustomers' 前綴

所以是這樣的:

http://www.myapp.appspot.com/deleteCustomers/NewUrl1 
http://www.myapp.appspot.com/deleteCustomers/SomethingElse 
http://www.myapp.appspot.com/deleteCustomers/YetAnotherUrlForTestpy 

的上面會被引導到test.py腳本所有。重定向到main.py腳本,你根本途徑別的什麼,只是在/ deleteCustomers

http://www.myapp.appspot.com/ThisGoesToMain 
http://www.myapp.appspot.com/deleteStores #also goes to main 
http://www.myapp.appspot.com/deleteStores/YetAnotherUrlForMain 

我希望這是你想要的。

+0

謝謝Tkingovr,我沒有在webapp2中設置路徑值。WSGIApplication函數。現在,它的工作,再次感謝! – BeNdErR 2013-03-13 11:52:33