{"id":13,"date":"2008-09-24T11:13:21","date_gmt":"2008-09-24T09:13:21","guid":{"rendered":"http:\/\/daniel.hepper.net\/blog\/?p=13"},"modified":"2008-09-26T00:19:44","modified_gmt":"2008-09-25T22:19:44","slug":"dynamic-paths-for-lazy-people","status":"publish","type":"post","link":"https:\/\/daniel.hepper.net\/blog\/2008\/09\/dynamic-paths-for-lazy-people\/","title":{"rendered":"Dynamic paths for lazy people"},"content":{"rendered":"<p>Once your Django project has to run on multiple machines, the absolute paths in <code>settings.py<\/code> will drive you nuts.<\/p>\n<p><a href=\"http:\/\/www.mahner.org\/weblog\/wiederverwendbare-django-projekte\/\">A suggested solution to enhance portability<\/a> (in German) is to define a <code>PROJECT_ROOT<\/code>.<\/p>\n<pre lang=\"python\">import os\r\nPROJECT_ROOT = os.path.dirname(__file__)<\/pre>\n<p>And then define absolute paths like this:<\/p>\n<pre lang=\"python\">MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'site_media')\r\nTEMPLATE_DIRS = (\r\n    os.path.join(PROJECT_ROOT, 'templates'),\r\n)<\/pre>\n<p>The downside is, typing <code>os.path.join(PROJECT_ROOT,'foo')<\/code> <del>twice<\/del> over and over again, will probably incur the wrath of the DRY gods. But don&#8217;t despair, we can do better:<\/p>\n<pre lang=\"python\">import os\r\nPROJECT_ROOT = os.path.dirname(__file__)\r\np = lambda *x: os.path.join(PROJECT_ROOT,*x)<\/pre>\n<p>Use it like this:<\/p>\n<pre lang=\"python\">MEDIA_ROOT = p('static')\r\nTEMPLATE_DIRS = (\r\n    p('templates'),\r\n)\r\nMY_NESTED_PATH = p('x','y')\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Once your Django project has to run on multiple machines, the absolute paths in settings.py will drive you nuts. A suggested solution to enhance portability (in German) is to define a PROJECT_ROOT. import os PROJECT_ROOT = os.path.dirname(__file__) And then define &hellip; <a href=\"https:\/\/daniel.hepper.net\/blog\/2008\/09\/dynamic-paths-for-lazy-people\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[33],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-django","tag-django"],"_links":{"self":[{"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":4,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":20,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions\/20"}],"wp:attachment":[{"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daniel.hepper.net\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}