简体   繁体   English

在 Google App Engine 中查找相对路径的好方法是什么?

[英]What's a good way to find relative paths in Google App Engine?

So I've done the trivial "warmup" apps with GAE.所以我用 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. ..ETC。 The controllers will be Python modules handling requests.控制器将是处理请求的 Python 模块。 They would then need to locate (Django-style) templates in associated folders.然后他们需要在相关文件夹中找到(Django 风格的)模板。 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.所以,在我上面的例子中,如果 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).我见过的用于解决 Python 路径的其他(非 GAE 特定)解决方案似乎相当重量级(例如将路径拆分为列表并进行相应操作)。 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. 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.我假设任何硬编码都不会启动,因为这些应用程序存在于 Google 的无限服务器场中。 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.正如 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.我常用的方法是使用上述方法生成模板目录的路径,并将其存储为我的 controller object 的成员,并提供一个“getTemplatePath”方法,该方法采用提供的文件名并将其与基本名称连接。

The dirname function returns an absolute path, use relative paths.目录名dirname返回绝对路径,使用相对路径。 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).使用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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是在Google App Engine中管理FlashData的良好框架? - What's a good framework for managing flashdata in google app engine? 对于带有App Engine的python的键的数组,什么是好的db属性? - What is the good db property, for an keys's array for python with App Engine? 通过AJAX将用户输入的文本发送到Google App Engine的最佳方法是什么? - What's the best way to send user-inputted text via AJAX to Google App Engine? 安排在Google App Engine中运行超过十分钟的任务的最佳方法是什么? - What's the best way of scheduling a task that runs over ten minutes in Google App Engine? 在app引擎上生成GUID的好方法? - Good way to generate GUIDs on app engine? Google的App Engine SDK和Cloud SDK之间有什么关系? - What is the relationship between Google's App Engine SDK and Cloud SDK? Google App Engine与Python 2.7一起使用setuptools有什么用 - What's the use of setuptools in Google App Engine with Python 2.7 Google App Engine api 的命名空间管理器的范围是什么? - What is the scope of the Google App Engine api's namespace manager? 什么是Google App Engine的数据存储“父密钥”? - What is Google App Engine's Datastore “parent key”? Google App Engine中的父级和引用属性之间有什么区别? - What's the difference between a parent and a reference property in Google App Engine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM