在Google App Engine中找到相对路径的好方法是什么? [英] What's a good way to find relative paths in Google App Engine?

查看:91
本文介绍了在Google App Engine中找到相对路径的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我用GAE完成了简单的热身应用程序。现在我想用更复杂的目录结构来构建一些东西。

  siteroot / 
模型/
控制器/
控制器1 /
controller2 /
...
模板/
模板1 /
模板2 /
...

..等。控制器将是Python模块处理请求。然后他们需要在相关文件夹中找到(Django风格)模板。我看到的大多数演示程序都是这样解析模板路径的:

  path = os.path.join(os.path .dirname(__ file__),'myPage.html')

... __文件__属性解析到当前正在执行的脚本。因此,在我上面的例子中,如果一个Python脚本在controllers / controller1 /中运行,那么'myPage.html'将解析到同一个目录 - controllers / controller1 / myPage.html - 我宁愿将我的Python代码和模板。



我一起入侵的解决方案感觉... hacky:
$ b $

  base_paths = os.path.split(os.path.dirname(__ file__))
template_dir = os.path.join(base_paths [0],templates)

所以,我只是截掉当前运行脚本路径的最后一个元素,并将模板目录附加到新的道路。我见过的用于解析Python路径的其他(非GAE特定的)解决方案看起来相当重要(例如将路径拆分为列表并相应地进行操作)。 Django似乎有这个答案,但我宁愿坚持GAE API,与创建一个完整的Django应用程序并修改GAE。



假设任何硬编码的东西都不是首发,因为这些应用都在Google的无限服务器场中。那么,什么是更好的方法?正如Toni所说,你不能使用相对路径,因为你不能保证从工作目录到应用程序目录的路径将保持不变。



正确的解决方案是使用os.path.split,或者使用例如:

  path = os.path.join(os.path.dirname(__ file__),'..','templates ','myPage.html')

我常用的方法是使用模板目录上述方法,并将其存储为我的控制器对象的成员,并提供一个getTemplatePath方法,该方法使用提供的文件名并将其与基本名称一起加入。


So I've done the trivial "warmup" apps with GAE. Now I'd like to build something with a more complex directory structure. Something along the lines of:

siteroot/
    models/
    controllers/
        controller1/
        controller2/
        ...
    templates/
        template1/
        template2/
        ...

..etc. The controllers will be Python modules handling requests. They would then need to locate (Django-style) templates in associated folders. Most of the demo apps I've seen resolve template paths like this:

path = os.path.join(os.path.dirname(__file__), 'myPage.html')

...the __ file __ property resolves to the currently executing script. So, in my above example, if a Python script were running in controllers/controller1/, then the 'myPage.html' would resolve to that same directory -- controllers/controller1/myPage.html -- and I would rather cleanly separate my Python code and templates.

The solution I've hacked together feels... hacky:

base_paths = os.path.split(os.path.dirname(__file__))
template_dir = os.path.join(base_paths[0], "templates")

So, I'm just snipping off the last element of the path for the currently running script and appending the template directory to the new path. The other (non-GAE specific) solutions I've seen for resolving Python paths seem pretty heavyweight (such as splitting paths into lists and manipulating accordingly). Django seems to have an answer for this, but I'd rather stick to the GAE API, vs. creating a full Django app and modifying it for GAE.

I'm assuming anything hard-coded would be non-starter, since the apps live on Google's infinite server farm. So what's a better way?

解决方案

You can't use relative paths, as Toni suggests, because you have no guarantee that the path from your working directory to your app's directory will remain the same.

The correct solution is to either use os.path.split, as you are, or to use something like:

path = os.path.join(os.path.dirname(__file__), '..', 'templates', 'myPage.html')

My usual approach is to generate a path to the template directory using the above method, and store it as a member of my controller object, and provide a "getTemplatePath" method that takes the provided filename and joins it with the basename.

这篇关于在Google App Engine中找到相对路径的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆