Django路径,在Windows中开发,部署在linux上 [英] Django paths, developing in windows, deploying on linux

查看:562
本文介绍了Django路径,在Windows中开发,部署在linux上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的本地Windows机器上开发Django应用程序,然后部署到托管的Linux服务器上。路径的格式在两者之间是不同的,并且在部署之前手动替换消耗的时间比它应该更多。我可以根据我的设置文件中的变量编写代码,如果是语句,但是我想知道是否有人对这种情况有最佳实践。

解决方案

p> Django图书建议使用 os.path.join (并在Windows上使用斜杠而不是反斜杠):

  import os.path 

TEMPLATE_DIRS =(
os.path.join(os.path.dirname(__ file__),'templates')。替换('\\','/'),

这是最好的解决方案,因为您可以轻松创建相似的路径。如果您有多个相对路径,则帮助函数将缩短代码:

  def fromRelativePath(* relativeComponents):
return os.path.join(os.path.dirname(__ file__),* relativeComponents).replace(\\,/)

如果需要绝对路径,您应该使用环境变量(与 os.environ [MY_APP_PATH] )组合与 os.path.join


I'm developing Django apps on my local windows machine then deploying to a hosted linux server. The format for paths is different between the two and manually replacing before deployment is consuming more time than it should. I could code based on a variable in my settings file and if statements but I was wondering if anyone had best practices for this scenario.

解决方案

The Django book suggests using os.path.join (and to use slashes instead of backslashes on Windows):

import os.path

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)

I think this is the best solution as you can easily create relative paths like that. If you have multiple relative paths, a helper function will shorten the code:

def fromRelativePath(*relativeComponents):
    return os.path.join(os.path.dirname(__file__), *relativeComponents).replace("\\","/")

If you need absolute paths, you should use an environment variable (with os.environ["MY_APP_PATH"]) in combination with os.path.join.

这篇关于Django路径,在Windows中开发,部署在linux上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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