2013-12-22 24 views
0

我想有這樣的型動物切入點

http://localhost:8080/ 

http://localhost:8080/problem2/ 

http://localhost:8080/problem3/ 

的結構...

我也希望有一個Python的結構是這樣

-src 

    - app.yaml 
    - main.py 
    - package_problem_2 
     - main.py 
    - package_problem_3 
     - main.py 

我會喜歡在我的網頁中爲differents文件夾設置不同的main.py。 我的意思是,如果我在http://mydomain:8080這是src/main.py要處理的請求。但是,如果我在http://localhost:8080/problem2,它應該是package_problem_2/main.py處理請求的那個。

這可能嗎?

+0

與論壇網站不同,我們不使用「謝謝」,或「任何幫助表示讚賞」,或在[so]上簽名。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be -removed-from-posts) –

+0

是的,它是可行的,查看[Documentation](https://developers.google.com/appengine/docs/python/config/appconfig)。當你嘗試一些東西時,來返回並編輯您的問題以顯示您的嘗試(如果您有任何課程上的困難,goodluck) –

回答

0

您使用的是webapp2框架嗎?

如果是這樣,請繼續閱讀...

您需要四個文件。爲簡單起見,所有位於您的應用程序的根文件夾:

app.yaml 
urls.py 
SampleController.py 
Sample.html 

您的app.yaml,你應該有這樣的事情:

handlers: 
- url: /.* 
    script: urls.ap 

這告訴AppEngine上路由所有網址模式urls.py.

然後在你的urls.py,你有這樣的結構:

import webapp2 
import SampleController 

#For each new url structure, add it to router. 
#The structure is [py filename].[class name inside py file] 
app = webapp2.WSGIApplication(debug=True) 
app.router.add((r'/', SampleController.SampleHandler)) 


def main(): 
    application.run() 

if __name__ == "__main__": 
    main() 

在你的問題,你有三種結構:/,/ problem2,/ problem3。他們會對應這些:

app.router.add((r'/', SampleController.SampleHandler)) 
app.router.add((r'/problem2', SampleController.SampleHandler2)) 
app.router.add((r'/problem3', SampleController.SampleHandler3)) 

這取決於你決定是否他們去同一個處理程序或不。

SampleController.py看起來是這樣的:

import webapp2 
import os 

class SampleHandler(webapp2.RequestHandler): 
    def get(self): 
     template_values = { 
      'handler': 'We are in SampleHandler', 
      'param2': param2 
     } 
     path = os.path.join(os.path.dirname(__file__), 'Sample.html') 
     self.response.out.write(template.render(path, template_values)) 


class SampleHandler2(webapp2.RequestHandler): 
    def get(self): 
     template_values = { 
      'handler': 'We are in SampleHandler2', 
      'param2': param2 
     } 
     path = os.path.join(os.path.dirname(__file__), 'Sample.html') 
     self.response.out.write(template.render(path, template_values)) 


class SampleHandler3(webapp2.RequestHandler): 
    def get(self): 
     template_values = { 
      'handler': 'We are in SampleHandler3', 
      'param2': param2 
     } 
     path = os.path.join(os.path.dirname(__file__), 'Sample.html') 
     self.response.out.write(template.render(path, template_values)) 

通知他們都去同一個Sample.html文件。

Sample.html只是標準的html代碼。

+0

這不完全是我想要的,因爲我想在'SampleHandler2'中定義(它位於另一個包中, 'main.py')這個處理程序所有可能的重定向。我的意思是,我想在該模塊中定義這個 'app = webapp2.WSGIApplication([('/ problem2 /',MainHandlerForProblem2)), ('/ problem2/thanks',ThanksHandlerForProblem2)],debug = True)' 由於它在「default」包中的main.py中 – Manuelarte