Python: Dynamically obtain absolute path of project_folder


/ Published in: Python
Save to your folder(s)

Hard-coding absolute paths is never ideal so here's a quick way to reference your project folder


Copy this code and paste it in your HTML
  1. ############################
  2. #This can be dynamically obtained
  3. ############################
  4. #TEMPLATE_DIRS +=('/Users/user_name/Desktop/django_project/templates/',)
  5.  
  6. ############################
  7. #With a little abspath magic
  8. ############################
  9. import os.path
  10. PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
  11. TEMPLATE_DIRS += (os.path.join(PROJECT_ROOT, "templates"),)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.