Login

map_path function for settings.py

Author:
btaylordesign
Posted:
July 23, 2011
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

By using map_path([directory_name]) you can avoid hard coding paths for a specific OS in your settings.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#settings.py

import os

def map_path(directory_name):
    return os.path.join(os.path.dirname(__file__), directory_name).replace('\\', '/')

STATIC_ROOT = map_path('static')
TEMPLATE_DIRS = map_path('templates')

"""
This would find the "static" and "templates" directories (given the structure below) without having to hard code the paths in settings.py. This is very useful if you switch environments from Windows to Mac or Linux from say a PC workstation at your office to a Mac at home.

Example Directory Structure:
my_app/
settings.py
templates/
static/
    images/
    css/
    js/
"""

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.