I'm attempting to get the Django development server to load a template with the following settings but it's throwing the error
TemplateDoesNotExist at /
homepage/index.html
Below are the files that I've edited to try to get this to work
blog/blog/settings.py - (Cut the portion pertaining to this question)
import os
TEMPLATE_DIRS = (
os.path.join( os.path.dirname(__file__), 'templates' ),
)
blog/apps/homepage/views.py
from django.shortcuts import render_to_response
def index(request):
return render_to_response('homepage/index.html')
Any thoughts about how to fix this?
By default Django uses two template loaders to locate templates:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
First the filesystem loader checks all dirs in TEMPLATE_DIRS to see if it can find the named template. If that fails the app_directories loader will look in the /templates/ dir in the current app for the named template. This way you can ship default templates with reusable apps that can easily be overridden.
In your case Django will look for the template here:
blog/blog/templates/homepage/index.html
blog/apps/homepage/templates/homepage/index.html
You give a wrong direction to your TEMPLATE_DIRS. The settings.py file is in package blog in project blog, but your templates folder is in blog project, not in blog package. You must give the absolute directory of your project and join templates with it.
in django 1.10, navigate to path/to/settings.py in your parent project directory, look for:
TEMPLATES = [.....] and specify the path/to/project_directory in the DIR: option
for example (in my own case):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/xampp/htdocs/advisory_portal'
],
'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',
],
},
},
]
Related
I am trying to add a django-oscar shop to an existing django website.
My problem is that the templates of the two are somehow clashing, such that I can either see the existing website, or the shop, but not both.
Here is the overarching urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from oscar.app import application
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('main.urls')),
# oscar
url(r'^shop/', include(application.urls)),
]
And in the settings:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# os.path.join(BASE_DIR, 'templates'),
OSCAR_MAIN_TEMPLATE_DIR
],
# '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',
'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata',
'main.context_processors.google_analytics'
],
'loaders': [
'django.template.loaders.app_directories.Loader',
'django.template.loaders.filesystem.Loader',
],
},
},
]
If I switch the order of the loaders, either the original website (in app 'main'), or the Oscar shop, can no longer be accessed/viewed. So I'm not sure what esoteric detail I'm overlooking, and the docs don't cover this. Cheers.
The issue you're having is that your template names conflict with Oscar's. Oscar has it's own base.html, which is what the template loader will find if you list that loader first, instead of your own base.html. Django will use the first one it finds.
This is a known issue with Oscar - unfortunately there is no backwards compatible way to fix it, and so it hasn't been addressed for some time.
Changing Oscar's behaviour is quite difficult so I'd suggest you try and change your template structure instead. Specifically, you should namespace all your app templates. So if your app is called myapp, then put the base template in myapp/templates/myapp/base.html. You would then refer to this in other templates as {% extends 'myapp/base.html' %}. Similarly put all other templates in templates/myapp/.
This will ensure that your templates do not clash with Oscar's, and the problem should go away.
I set an address for the DIRS in TEMPLATES setting in my project's settings.py file and also i change APP_DIRS to False.
now, all of the templates will running but when i go to localhost:8000/admin, it generates an error that said "Template Does Note Exists".
TemplateDoesNotExist at /admin/
admin/index.html
i dont want to APP_DIRS be True.
how can i solve this problem?
this is the settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': False,
'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',
],
},
},
]
If you dont want APPS_DIR to be TRUE, copy templates of admin app to your template directory. When you set APP_DIR to True django will check for a directory named templates in each apps and if found load specified templates from that directory. Else django will check for a directory with same name as of app in the global template directory if found render specified template.
In you case APP_DIR is set to False and there is no directory named as admin in global template folder. So copy template directory from django/contrib/admin/ of your virtual environment and place it in your global template directory. I suppose your global template directory is in the project directory itself.
os.path.join(BASE_DIR, 'templates'),
HTH :)
I read many different setup for getting allauth working with django in general, and 1.8 specifically but none is working.
here is my current setup with allauth 0.29:
TEMPLATES = [
{
'BACKEND' : 'django.template.backends.django.DjangoTemplates',
'DIRS': [ os.path.join( BASE_DIR, 'templates' ), os.path.join( BASE_DIR, 'templates', 'allauth' ) ],
'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',
],
},
},
]
With all Allauth accounts templates in
basedir/templates/allauth/accounts
Altough all allauth template pickup the site's base template, any modification to the template in tis directory are royally ignored. if you have this working with django 1.8 please describe your setup.
To override templates you must put a folder with the name of the app inside of your templates folder. In your case you want something like:
templates/account
It turns out that you can only override templates in a django APP template dir. Not the main template dir, even if properly configured.
And that app must be loaded before all the allauth app in your settings.py .
That's weird since the allauth templates are supposed to affect site wide authentication process, not just a specific app in a project.
I ended up create an app "allauthapp" specifically for overriding templates.
I am using the default authentication system of Django (v1.8.12), but I want to create my own templates (actually according to the documentation django.contrib.auth do not provide any template).
Then I added the folder 'registration' inside the 'templates' folder of my application 'mainApp'. According to the documentation this is the right place, a folder named 'registration'.
I have also created the template 'login.html', and this template is loaded properly at the defined url '...account/login'.
The problem is with the other templates:
logged_out.html
password_change_done.html
password_change_form.html
password_reset_complete.html
password_reset_confirm.html
password_reset_done.html
password_reset_email.html
password_reset_form.html
The previous templates are been loaded from the 'admin' application and not from my app.
How can I tell Django that my 'registration' templates should be loaded and not the templates defined in the admin application?
My project url.py is
urlpatterns = [
url(r'^', include('mainApp.urls', namespace="mainApp")),
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('django.contrib.auth.urls')),
]
My template settings are the default settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'debug': True,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Just move mainApp above django.contrib.admin in your INSTALLED_APPS setting, so that Django searches the mainApp/templates directory first.
That way, you don't need to add mainApp/templates to your DIRS setting.
I am following part 2 of the Django tutorial. I am trying to override an admin template (base_site.html)
I copied the file from the django/contrib/admin/templates to mytemplates/admin/base_site.html
I also updated settings.py:
#Base Directory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
#Template directories
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)
I tried putting the mytemplates folder in the root of the project folder as well as in the mysite folder with no luck. Any pointers would be great!
EDITED PREVIOUS USER RESPONSE -- THIS WORKS:
I think your relative path to the templates directory is wrong.
If you follow these steps it should work: (I tested it myself)
Put the mytemplates dir side by side with the manage.py file
project
-app1
-app2
-mytemplates
-admin
-base_site.html
-manage.py
Change the TEMPLATE_DIRS to:
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)
Make sure the order of the template loader is:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
#YardenST's answer almost worked for me. I guess it's a matter of configuration.
In case you run into trouble, I suggest you use this line:
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)
Next, put a breakpoint to show the actual outcome, or alternatively use print TEMPLATE_DIRS.
That's where you should place the templates you want to override.
#kat-russo, thx ;)
I tried to setup admin templates according to docs
project_name
-app1
-app2
-project_name //main folder -> settings.py , urls.py, wsgi.py
-templates
-admin
-project_name
base.html
without success, but
-templates
-admin
base.html
works for me.
my config (Django 1.10.4 w/Django CMS 3.4.1)
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',
'sekizai.context_processors.sekizai',
'cms.context_processors.cms_settings',
],
},
},
]
You can override all templates.
Create an admin directory in templates and add the files.
The all files.
https://github.com/django/django/tree/master/django/contrib/admin/templates/admin