官术网_书友最值得收藏!

Defining relative paths in the settings

Django requires you to define different file paths in the settings, such as the root of your media, the root of your static files, the path to templates, the path to translation files, and so on. For each developer of your project, the paths may differ as the virtual environment can be set up anywhere and the user might be working on Mac OS X, Linux, or Windows. Anyway, there is a way to define these paths that are relative to your Django project directory.

Getting ready

To start with, open settings.py.

How to do it…

Modify your path-related settings accordingly instead of hardcoding the paths to your local directories, as follows:

# settings.py
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
import os

BASE_DIR = os.path.abspath(
    os.path.join(os.path.dirname(__file__), "..")
)

MEDIA_ROOT = os.path.join(BASE_DIR, "myproject", "media")

STATIC_ROOT = os.path.join(BASE_DIR, "myproject", "static")

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "myproject", "site_static"),
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, "myproject", "templates"),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, "locale"),
)

FILE_UPLOAD_TEMP_DIR = os.path.join(
    BASE_DIR, "myproject", "tmp"
)

How it works…

At first, we define BASE_DIR, which is an absolute path to one level higher than the settings.py file. Then, we set all the paths relative to BASE_DIR using the os.path.join function.

See also

  • The Including external dependencies in your project recipe
主站蜘蛛池模板: 柏乡县| 黑龙江省| 西充县| 大宁县| 阿瓦提县| 米易县| 方城县| 西城区| 清丰县| 榆社县| 顺昌县| 武威市| 泰宁县| 密云县| 泰和县| 田东县| 安国市| 金寨县| 姚安县| 错那县| 凤山市| 虹口区| 平南县| 嘉黎县| 新营市| 穆棱市| 宝山区| 达日县| 吴旗县| 威信县| 焉耆| 涿鹿县| 固镇县| 凤城市| 五大连池市| 阳泉市| 湟中县| 金门县| 淮安市| 遂宁市| 福贡县|