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

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
主站蜘蛛池模板: 开远市| 光山县| 双鸭山市| 施秉县| 始兴县| 开江县| 遵化市| 定西市| 诸城市| 云林县| 上饶市| 台湾省| 万年县| 应用必备| 永靖县| 泽库县| 丰镇市| 清徐县| 崇州市| 那坡县| 永泰县| 赣州市| 湘乡市| 特克斯县| 诸城市| 利辛县| 梅河口市| 通海县| 英德市| 清原| 青铜峡市| 阳朔县| 红安县| 新龙县| 新河县| 信丰县| 麟游县| 沙雅县| 从化市| 那坡县| 勐海县|