1

This question was answered more or less here but it didn't work to me and as far I can see more people have the same problem.

In my settings.py I have this lines:

 #TEMPLATE_DIRS = [os.path.join(BASE_DIR,'..', 'templates'),]
 TEMPLATE_DIRS = ("/root/GODJANGO/thedjango/django_project",)

The comented line didn't work. It works if I write the full path but It's not professional and I don't wanna have problems when I three months later I migrate my server because I will not remember this thing.

  1. Can anybody tell me how to write My Path correctly ("dynamically, I mean")
  2. Please tell me where is the best directory to put my templates folder and also my admin templates folder
Community
  • 1
  • 1
BugFixer
  • 293
  • 1
  • 4
  • 15

3 Answers3

1

If you have the following project layout:

manage.py
myproject/
          settings.py
          urls.py
          wsgi.py
          templates/
                   admin/
app1/
app2/

Then you can dynamically set your template directory by putting the following in your settings.py:

...
SETTINGS_PATH = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, "templates")
)
...

if your template folder is in the parent folder to the settings.py you will need something like:

...
SETTINGS_PATH = os.path.abspath(os.path.dirname(__file__))
PROJECT_FOLDER = (os.path.split(SETTINGS_PATH))[0]  # get the parent directory
TEMPLATE_DIRS = (
    os.path.join(PROJECT_FOLDER, "templates")
)
...

As you can see, we are manually traversing the file tree to find where the templates folder is and assigning it dynamically.

The best place for your templates folder depends on your project layout (< 1.4 or >= 1.4) but it would be probably safest to say that it should be alongside your settings.py file. Your admin template folder will go inside your base templates folder: templates/admin/.

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • That work ! Thanks! I would like to vote your answer up but I have no enough reputation to do that, so if you vote my question up I will give you gratefully the up vote. "Gracies" (; – BugFixer Feb 06 '14 at 13:31
  • If it worked you can accept it as the correct answer by clicking the tick beside the upvote buttons. – Timmy O'Mahony Feb 06 '14 at 13:33
1

What are you trying to do with this paramater ".." in the commented line?

if it is means parent directory you can use os.path.dirname(BASE_DIR)

Second part:

It depends you or team work with; i love to keep templates directory in every app's directory (and if you do like this you don't need to define TEMPLATE_DIRS). I mean if i have templates of news app, they goes ../news/templates/. But this time my friend (front-end developer) says i cant find them, can we put all of them in one place? so i put them in one directory with sub directories (../templates/news/). This main templates directory is in main project directory (near the manage.py file). And if you add this main directory to INSTALLED_APPS (because its kind an app) you don't need to define TEMPLATE_DIRS too. And even you can create models.py admin.py files here.

iskorum
  • 1,137
  • 1
  • 16
  • 27
  • Thanks for the explanation, the truth is that I'm a very recent user in Django so I'm only following the official tutorials by the moment and I want to do the right thing and be sure about what way I take. Regards! – BugFixer Feb 06 '14 at 13:36
0

Considering the second part of your question and according to this the safest part for your templates is to create a templates dir under your projects main directory (e.g. blog).

As for the first part i am not sure.

  • I will appreciate if you tell me in what minute it is said, thanks! – BugFixer Feb 06 '14 at 13:32
  • @BugFixer i just saw the comment sorry for the delay, however as i am not really experienced too i recommend to watch the whole tutorial. Project structure can be a pain sometimes. –  Feb 09 '14 at 19:19