If your like me, you spend time moving your code between your test and production servers. This can easily complicate issues when you have hard coded full paths in your applications. There is an easy way to solve this issue – mainly I use it with the templates in Django, but you should be a able to use it anywhere.
Let’s do this in a few easy steps – the best way to do anything!
1. Open your settings.py file in your application and find the template directives, this should be an array that starts with TEMPLATE_DIRS=(, now insert the following code just above the TEMPLATE_DIRS to import the required library to make this function
import os.path
2. Inside the TEMPLATE_DIRS array add the following to keep your path updated no matter where your code may go! You can do this for each
os.path.join(os.path.dir_name(__file__), 'templates'),
So, you final code should look something like this:
import os.path
TEMPLATE_DIRS(
os.path.join(os.path.dir_name(__file__), 'templates'),
)
If you are planning on using this code in other system setting arrays I would just include the import statement once, at the top of the page – actually you might want to do that anyways. Your choice.