Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set LOCALE_PATH relative to project path in Django?

In the development environment, I set locale paths to:

LOCALE_PATHS = (
'/Users/***/Documents/Projects/**/Server/Django/**/locale',
)

But when I deploy it to server, locale path shold be changed.

How can I handle this?

like image 935
Burak Avatar asked Feb 12 '14 17:02

Burak


2 Answers

to settings add

import os

LOCALE_PATHS = (
    os.path.join(os.path.dirname(__file__), "locale"),
)
like image 63
Ondrej Sika Avatar answered Nov 15 '22 08:11

Ondrej Sika


I am still currently using Django 1.5 and have found that I can handle it the easiest with the following:

LOCALE_PATHS = (
    'locale',
)

The following works better if you need to use an absolute path (indentation emphasized on purpose):

import os.path

LOCALE_PATHS = (
    os.path.abspath(
        os.path.join(
            os.path.dirname(__file__), 
                '..', "locale")),
)
  • First, the call to os.path.dirname returns the path to the directory of the settings file (__file__), e.g. /Users/foobar/projects/django-tutorial/mysite/mysite
  • Next, the call to os.path.join joins the previous result with a relative reference to a locale directory one level higher, e.g. /Users/foobar/projects/django-tutorial/mysite/mysite/../locale
  • Last, the call to os.path.abspath transforms the previous relative path reference to an absolute one, e.g. /Users/foobar/projects/django-tutorial/mysite/locale
like image 27
Jarno Lamberg Avatar answered Nov 15 '22 09:11

Jarno Lamberg