I am trying to create a login page in my django application. I created a "templates" folder on the root directory of my application.
Then on my settings.py I wrote this code.
TEMPLATE_DIRS = (os.path.join(BASE_DIR,'templates'),)
And it is giving this feedback:
TemplateDoesNotExist at /login/
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Users/julianasakae/Desktop/DjangoProject/demo/lib/python3.4/site-packages/django/contrib/admin/templates/login.html (File does not exist)
/Users/julianasakae/Desktop/DjangoProject/demo/lib/python3.4/site-packages/django/contrib/auth/templates/login.html (File does not exist)
/Users/julianasakae/Desktop/DjangoProject/boardgames/main/templates/login.html (File does not exist)
I tryed everything, it does not seems to work.
Any suggestions?
What version of Django are you using? It appears that TEMPLATE_DIRS was used prior to 1.8 but in the current version it has changed to a DIRS option in the TEMPLATES setting.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/home/html/templates/lawrence.com',
'/home/html/templates/default',
],
},
]
Template DIRS Option Docs
Well it is not a good solution, but try hardcoding the full path.
Related
I followed this Django tutorial: https://docs.djangoproject.com/en/1.10/intro/reusable-apps/.
I have a project called oldcity and an app called oldantwerp. The app is located in a parent directory called django-oldantwerp and the app directory itself has a subdirectory templates. The index.html file that my project is looking for is situated like so:
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html
I tried to use this app with my project by including it in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'oldantwerp'
]
and in urls.py (in the projec), like so:
urlpatterns = [
url(r'^oldantwerp/', include('oldantwerp.urls')),
url(r'^admin/', admin.site.urls),
]
When I go to the admin page, everything works, but when I try to open the index page I get this error:
TemplateDoesNotExist at /oldantwerp/
It says it tried to locate the index.html file like so:
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/admin/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/auth/templates/oldantwerp/index.html (Source does not exist)
And it also tried searching for another file: place_list.html, which is strange because I don't think I have such a file.
What could be wrong?
EDIT
This is the code of views.py in the oldantwerp folder:
class IndexView(generic.ListView):
template_name = 'oldantwerp/index.html'
context_object_name = 'places'
def get_queryset(self):
return Place.objects.order_by('id')[:]
EDIT
Maybe worth mentioning: it all did work when I just had a folder oldantwerp as a subdirectory in the oldicty project folder. This error only occurred after I started implementing it from an external package.
EDIT
These are my template settings in settings.py:
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',
],
},
},
]
One most likely problem could be you have got the template name wrong in your view. Ensure you use the template oldantwerp/index.html in the view, not just index.html.
If you look at your stacktrace you will see that you application tries to load the template from: * django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist).
If I read your first statement correctly your directory structure is:
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html.
So you can either create a directory structure proper for django to identify your templates directory or you can update your templates constant from the settings file and point django to a physical location where it can find your templates.
You can try to set DIRS in settings.py like this: os.path.join(BASE_DIR, 'templates'), delete the oldantwerp folder from your templates folder and add index.html in templates.
After that, edit template_name = 'oldantwerp/index.html' to template_name = 'index.html'
Is it possible to set the django project template loading priority so that first of all it loads the apps' "templates" folder and after that the project "templates". If the template exists in the app folder, then use it. If it does not exist in the app folder, then try load from project folder.
Or it is not normal way to load templates?
I ask because I see in the exception, that Django tries to load first of all global templates:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: D:\myprojects\my-website\src\templates\home.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\User\Python27\lib\site-packages\django\contrib\admin\templates\home.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\User\Python27\lib\site-packages\django\contrib\auth\templates\home.html (Source does not exist)
django.template.loaders.app_directories.Loader: D:\myprojects\my-website\src\website\templates\home.html (Source does not exist)
Update your TEMPLATES setting, and put the app_directories loader before the filesystem loader.
If you currently have 'APP_DIRS': True, you will have to remove this, and add the loaders option.
For example, you could change your TEMPLATES setting to:
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'OPTIONS': {
'loaders': [
'django.template.loaders.app_directories.Loader',
'django.template.loaders.filesystem.Loader',
],
},
}]
i'm new in python world and i'm using python 3.4 and django 1.7
When o put DEBUG=True in settings.py the browser shows me an error like:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\workspace_virtualenv34\prog\prog\templates\main_sites\registration\login.html (File does not exist)
But my templates are in a different path:
C:\workspace_virtualenv34\prog\templates\
In settings.py i set ROOT_PATH as:
ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname( file ), os.pardir))
Is there anyway to turn back path levels that solve this problem?
Thanks.
You should be able to provide any directories to TEMPLATE_DIRS that django will use to search for templates
https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs
you should be able to construct this using the ROOT_PATH to avoid referencing absolute paths directly; change for your specific path:
TEMPLATE_DIRS = (
os.path.join(ROOT_PATH, '..', '..', 'prog', 'templates'
)
This is driving me crazy. I've done something weird and it appears that my TEMPLATE_DIRS entries are being ignored. I have only one settings.py file, located in the project directory, and it contains:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'web_app/views/'),
)
I'm putting project-level templates in the /templates folder, and then have folders for different view categories in my app folder (e.g. authentication views, account views, etc.).
For example, my main index page view is in web_app/views/main/views_main.py and looks like
from web_app.views.view_classes import AuthenticatedView, AppView
class Index(AppView):
template_name = "main/templates/index.html"
where an AppView is just an extension of TemplateView. Here's my problem: when I try to visit the page, I get a TemplateDoesNotExist exception and the part that's really confusing me is the Template-Loader Postmortem:
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django\contrib\admin\templates\main\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\main\templates\index.html (File does not exist)
Why in the world are the 'templates' and 'web_app/views' directories not being searched? I've checked Settings via the debugger and a breakpoint in views_main.py and it looks like they're in there. Has anyone had a similar problem? Thanks.
What version of Django are you using? TEMPLATE_DIRSis deprecated since 1.8
Deprecated since version 1.8:
Set the DIRS option of a DjangoTemplates backend instead.
https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs
So try this instead:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Here's a link to an upgrade guide: https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/
I included the path to my templates folder in the settings.py file by setting:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
and likewise for STATIC_FILES_DIR, but I keep getting the TemplateDoesNotExist error. When I look at the postmortem of the error, I see:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\home.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\home.html (File does not exist)
Why would the template.loader be looking for the template files in that directory instead of the one specified in my settings.py file? Also, when I copy over my home.html page into the first directory mentioned in the error message, the page loads the content without error, so how can I get the loader to move from the location that it's searching to the directory where the files are actually located?
Valid setting name is TEMPLATE_DIRS and it is a tuple of strings, not the simple string:
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )