2

I'm struggling to get the following working:

I would like to have a 'general' templates folder for non-app-specific html in the directory of my project, akin to the static folder for non-app-specific static files.

My current structure looks like this:

|- my_app/ |- dashboard/ |- static |- dashboard/ |- css/ |- ... |- templates |- dashboard |- index.html |- ... |- urls.py |- views.py |- landing/ |- static |- landing/ |- css/ |- ... |- templates |- landing |- index.html |- ... |- urls.py |- views.py |- my_app/ |- static/ |- my_app/ <-- no problem loading these |- css/ |- ... |- templates |- my_app <-- unable to load these |- boilerplate.html |- settings.py |- ... |- manage.py 

My current convention is that if the html or static files are in an app directory, they are specific to that app, if they are in the project (here my_app) directory, they are applicable across the whole project.

My problem now is that when I try to load boilerplate.html (a snippet) into dashboard/index.html by stating {% include "my_app/boilerplate.html" %} in dashboard/index.html, it complains with:

TemplateDoesNotExist at /dashboard

My settings.py file, or at least the part I believe to be relevant is the following:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(BASE_DIR, '/my_app/templates').replace('\\', '/'), ], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] 

Something is most likely missing or wrongly configured as it is not working, but I cannot figure out what it is. Any ideas?

1
  • well I think you are using windows and now to your question you should use one template folder for many app os.path.join(BASE_DIR, '/templates').replace('\\', '/'), remove my_app from settings then make folder app wise in the templates folder and also same apply for static and media files here my_app is your main project so that will work fine but django can't able to get from other apps Commented Sep 23, 2017 at 12:40

2 Answers 2

2

well I think you are using windows and now to your question you should use one template folder for many apps in your project

os.path.join(BASE_DIR, '/templates').replace('\', '/'),

or

'DIRS': [BASE_DIR+"/templates"],

remove my_app from settings

then make folder app wise in the templates folder and also same apply for static and media files.

Here my_app is your main project so that will work fine but Django can't able to get from other apps

hope this helps tell me if you need more explanation on this :)

Sign up to request clarification or add additional context in comments.

2 Comments

Using just one folder for all templates sounds very counterintuitive and in my personal experience becomes very annoying to work in once the project starts growing. I'd hate having to scroll through my project directory tree in my IDE every time I'm working on an app. Isn't there a way I can just tell Django "look into the application templates folders AND in my_app/templates", like it does with the static folders?
yes, I agree with you but you can always make subfolders as applications are added to the project and this is the best approach Django provides. I've also done a large project with 9 applications and almost more than 90 templates.
0

It turns out my mistake was a stupid one. In my settings.py file, where it says:

os.path.join(BASE_DIR, '/my_app/templates').replace('\\', '/'), 

It should have said:

os.path.join(BASE_DIR, 'my_app/templates').replace('\\', '/'), 

or the concatenation would fail.

Ugh.

The remainder of settings.py file, as shown in the OP, is valid. It now works under Django 1.11.

Well, thanks anyway.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.