django项目根本身发现 [英] django project root self discovery

查看:77
本文介绍了django项目根本身发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我记得有一些命令可以放在 settings.py 文件中,这样基本上当你将django项目移动到另一个目录时,它不会得到foo-bar'd up。

Ok so I recall there are some commands you could put in the settings.py file so that basically when you move your django project to another directory it won't get foo-bar'd up.

我知道我可以通过在每个地方提到一个字符串变量来提供主目录,但是有一个更优雅的方式来做这个?

I know I could just do this by having a string variable everywhere it mentions the home directory but is there a more elegant way of doing this?

推荐答案

抓住 __文件__ 全局,并使用各种功能 os.path

Grab the __file__ global, and use the various functions in os.path on it.

import os.path
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

这篇关于django项目根本身发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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