Working with Django 1.8
I have two templates with same name,
base.html
at /mysite/templates/base.html
and another from an app with the same name (base.html) at location, /mysite/app/templates/base.html
my template settings,
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
},
},
]
view
def home(request):
c = { name: 'myname', username: 'myusername'}
return render(request, 'base.html', c)
This calls the 'base.html' from the project root, but how can I call the template from the app/template
directory.
thank you for any reference!
Update:
I've added the app location to the template dirs,
'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'app/templates')],
now my IDE shows both the files, still the root's base.html
is rendered. how do I reference one template over the other?
Update: Fixed with adding one more directory at the app template level.
now my app templates directory structure looks like this,
mysite/app/templates/app/base.html
os.path.join(BASE_DIR, 'app/templates')
is not needed in TEMPLATE DIR if the app is in the installed apps tuple.
more efficient solutions on Django 1.8 are welcome.
With your current directory structure, you can't - it's done this way to allow you to override templates by apps later on in your INSTALLED_APPS
setting. The convention is to use a directory structure more like:
app1/templates/app1/base.html
app2/templates/app2/base.html
And then use app1/base.html
or app2/base.html
to refer to the one you want.
Edit
To be more clear on what I mean: your project structure could look like this:
mysite
- templates
- base.html
- manage.py (etc...)
- app
- models.py (etc...)
- templates
- app
- base.html
Then you can use base.html
to get the first one and app/base.html
to get the other - and the relevant app templates are all packaged inside the app they refer to, but are namespaced.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments