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:
所以我用GAE完成了琐碎的“热身”应用程序。现在我想用更复杂的目录结构构建一些东西。有点像:
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:
..等等。控制器将是处理请求的Python模块。然后,他们需要在相关文件夹中找到(Django风格)模板。我见过的大多数演示应用都解决了这样的模板路径:
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.
... __ file __属性解析为当前正在执行的脚本。所以,在上面的例子中,如果一个Python脚本在controllers / controller1 /中运行,那么'myPage.html'会解析到同一个目录 - controllers / controller1 / myPage.html - 而我宁愿干净地分开我的Python代码和模板。
The solution I've hacked together feels... hacky:
我一起入侵的解决方案感觉...... 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.
所以,我只是剪掉当前正在运行的脚本的路径的最后一个元素,并将模板目录附加到新路径。我见过的用于解析Python路径的其他(非GAE特定的)解决方案似乎非常重量级(例如将路径拆分为列表并相应地进行操作)。 Django似乎有一个答案,但我宁愿坚持GAE API,而不是创建一个完整的Django应用程序并为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?
我假设任何硬编码都不是首发,因为这些应用程序存在于Google的无限服务器场中。那么更好的方法是什么?
2 个解决方案
#1
4
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.
您不能像Toni建议的那样使用相对路径,因为您无法保证从工作目录到应用程序目录的路径将保持不变。
The correct solution is to either use os.path.split, as you are, or to use something like:
正确的解决方案是使用os.path.split,或者使用类似的东西:
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.
我通常的方法是使用上面的方法生成模板目录的路径,并将其存储为我的控制器对象的成员,并提供一个“getTemplatePath”方法,该方法获取提供的文件名并将其与基本名称连接。
#2
1
The dirname
function returns an absolute path, use relative paths. See what is the current directory when your controllers are executed with os.path.abspath(os.path.curdir)
and build a path to the templates relative to that location (without the os.path.abspath
part of course).
dirname函数返回绝对路径,使用相对路径。使用os.path.abspath(os.path.curdir)执行控制器时,查看当前目录是什么,并构建相对于该位置的模板路径(当然没有os.path.abspath部分)。
This will only work if the current directory is somewhere inside siteroot, else you could do something like this:
这只有在当前目录位于siteroot内的某个地方时才有效,否则你可以这样做:
template_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, "templates")
#1
4
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.
您不能像Toni建议的那样使用相对路径,因为您无法保证从工作目录到应用程序目录的路径将保持不变。
The correct solution is to either use os.path.split, as you are, or to use something like:
正确的解决方案是使用os.path.split,或者使用类似的东西:
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.
我通常的方法是使用上面的方法生成模板目录的路径,并将其存储为我的控制器对象的成员,并提供一个“getTemplatePath”方法,该方法获取提供的文件名并将其与基本名称连接。
#2
1
The dirname
function returns an absolute path, use relative paths. See what is the current directory when your controllers are executed with os.path.abspath(os.path.curdir)
and build a path to the templates relative to that location (without the os.path.abspath
part of course).
dirname函数返回绝对路径,使用相对路径。使用os.path.abspath(os.path.curdir)执行控制器时,查看当前目录是什么,并构建相对于该位置的模板路径(当然没有os.path.abspath部分)。
This will only work if the current directory is somewhere inside siteroot, else you could do something like this:
这只有在当前目录位于siteroot内的某个地方时才有效,否则你可以这样做:
template_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, "templates")