python - What's a good way to find relative paths in Google App Engine? -
so i've done trivial "warmup" apps gae. i'd build more complex directory structure. along lines of:
siteroot/ models/ controllers/ controller1/ controller2/ ... templates/ template1/ template2/ ...
..etc. controllers python modules handling requests. need locate (django-style) templates in associated folders. of demo apps i've seen resolve template paths this:
path = os.path.join(os.path.dirname(__file__), 'mypage.html')
...the __ file __ property resolves executing script. so, in above example, if python script running in controllers/controller1/, 'mypage.html' resolve same directory -- controllers/controller1/mypage.html -- , rather cleanly separate python code , templates.
the solution i've hacked feels... hacky:
base_paths = os.path.split(os.path.dirname(__file__)) template_dir = os.path.join(base_paths[0], "templates")
so, i'm snipping off last element of path running script , appending template directory new path. other (non-gae specific) solutions i've seen resolving python paths seem pretty heavyweight (such splitting paths lists , manipulating accordingly). django seems have answer this, i'd rather stick gae api, vs. creating full django app , modifying gae.
i'm assuming hard-coded non-starter, since apps live on google's infinite server farm. what's better way?
you can't use relative paths, toni suggests, because have no guarantee path working directory app's directory remain same.
the correct solution either use os.path.split, are, or use like:
path = os.path.join(os.path.dirname(__file__), '..', 'templates', 'mypage.html')
my usual approach generate path template directory using above method, , store member of controller object, , provide "gettemplatepath" method takes provided filename , joins basename.
Comments
Post a Comment