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/
.
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.
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.
- [Answered ]-Django: how to make a link on main page to sub-pages
- [Answered ]-Django F() objects and custom saves weirdness
- [Answered ]-Get queryset for ModelMultipleChoiceField field based on ModelChoiceField
- [Answered ]-Toggle element by numerical value of data attribute in django template
- [Answered ]-Python regular expression not matching file contents with re.match and re.MULTILINE flag