Template loading locations - django

i am newbie to django and learning by doing small application.
in views.py i have intentionally included a template file(.html) which does not exist.
t = get_template('current_datetimeP.html')
in setting.py
TEMPLATE_DIRS = (
"D:/Django_Code/mysite/templates/",
)
i get this error TemplateDoesNotExist at /time/
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
D:\Django_Code\mysite\templates\current_datetimeP.html (File does not exist)
since i have included D:\Django_Code\mysite\templates\ in settings.py its fine that the Django is loking in there but what about following
I also get this
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\auth\templates\current_datetimeP.html (File does not exist)
Why Django looking in here *C:\Python27\lib\site-packages\django\contrib\auth\templates*.
Is it a default location to search templates ?
one more thing , its is evident that Django is looking in above directory. can we place templates file in there instead of putting in application directory. (Although it is not a good practice ) ?

Template loading is controlled by the TEMPLATE_LOADERS setting in settings.py, which usually looks something like this:
TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.Loader',
'django.template.loaders.filesystem.Loader',
)
The app directories loader looks for templates in a "templates" folder in any of the apps listed in INSTALLED_APPS in settings.
The filesystem loader attempts to load templates from the fully qualified directory path names listed under TEMPLATE_DIRS in the settings file.
If you wish to give the filesystem loader preference, let it come before the app directories loader in the TEMPLATE_LOADERS setting. If you'd like to turn off app directories loading, comment it out with a # ( or delete it from the tuple ).

try using template path created with settings.py location
import os
DIRNAME = os.path.dirname(__file__)
TEMPLATE_DIRS = (
os.path.join(DIRNAME, "templates"),
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
)

Related

Django Template Does Not Exist

I know this has been covered already but none of the solutions have worked for me. I have a basic django project and it cannot find the template. Here is my views.py:
now = datetime.datetime.now()
t = get_template('index.html')
html = t.render(Context({'current_date' : now}))
return HttpResponse(html)
This is my TEMPLATE_DIRS in settings.py. The project is specified under INSTALLED_APPS
TEMPLATE_DIRS = (
'/Users/Howie/Desktop/djangoCode/mysite/Movies/templates/',
)
This is the error I get every time
Template-loader postmortem:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\Users\Howie\Desktop\djangoCode\mysite..\templates\index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django-1.7.2-py3.4.egg\django\contrib\admin\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django-1.7.2-py3.4.egg\django\contrib\auth\templates\index.html (File does not exist)
C:\Users\Howie\Desktop\djangoCode\mysite\Movies\templates\index.html (File does not exist)
I don't understand why django cant find my index.html file. I've tried moving the templates folder into the app itself(named Movies) and I've also had it in the root project folder but both of those didn't work. Any insight would be helpful.
The app_directories.Loader is enabled by default. This means you can put your template in an app/templates directory and be done. No need for TEMPLATE_DIRS and/or TEMPLATE_LOADERS settings. Just out of the box Django:
project/app/views.py
project/app/templates/index.html
get_template('index.html')
It's even better to use subdirectories:
project/app/views.py
project/app/templates/app/index.html
get_template('app/index.html')
This to avoid a index.html from some other app to take precedence.
If you really want a template folder in the root of your project, than don't use hardcoded paths. Do something like this:
from os.path import abspath, dirname, join, normpath
from sys import path
SITE_ROOT = dirname(dirname(abspath(__file__)))
TEMPLATE_DIRS = (
normpath(join(SITE_ROOT, 'templates')),
)
i had the same problem only to find out it was a typo error, i wrote templates
template/file_name/
as template which is not the Django way. The "s" is necessary
templates/file_name/

PyCharm does not resolve templates nor template tags nor statics in Django project

PyCharm does not find templates, nor template tags, nor static files in my Django project even though the project itself is set up right and working.
I am using Django 1.6.2 with this layout:
proj
.devtmp
manage.py
proj
settings.py
app1
templatetags
app2
templates
static
and with settings like these:
from os.path import join, dirname, pardir, abspath
PROJECT_ROOT = abspath(join(dirname(__file__), pardir))
DEV_TMP_DIR = join(PROJECT_ROOT, pardir, '.devtmp')
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (
join(PROJECT_ROOT, 'templates'),
)
MEDIA_ROOT = join(DEV_TMP_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = join(DEV_TMP_DIR, 'static')
STATIC_URL = '/static/'
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
'proj'
'proj.app1'
'app2'
)
Update:
In the IDE preferences, I have configured paths to the project root, settings.py and manage.py. In addition, I have configured the project interpreter (I am running the runserver from the IDE right now with no problems).
Please try this - it works for me for templates:
set templates directories in Python Template Languages -> Template directories
in Project Structure mark your apps as Source Folders
EDIT :
After a project structure reorganization I had problem with static files again. Setting destination of setting.py file in Django Support -> Settings resolved the issue.
Right click on the templates directory and "Mark Directory As" -> "Template Directory" and select template language as django
I've solved this problem by editing settings for Django framework, as seen on a picture.
You need to set up your project properly.
Django Set Up
In Preferences | Languages & Frameworks | Django, Enable Django Suport, set up the path to your project's root, settings.py file, and manage.py file:
Mark your templates directory as Templates
In File | Settings | Project Structure for Windows/Linux and
PyCharm | Preferences | Project Structure for macOS, choose the directory to be marked as a template root.
Click on Templates on Mark as.
Click on OK to apply the changes you made.
The issue for me was that I was doing this in my settings module:
INSTALLED_APPS = [*INSTALLED_APPS, "debug_toolbar"]
This caused PyCharm to think that debug_toolbar is the only app in the entire project. Declaring INSTALLED_APPS in the standard way solved it.

TemplateDoesNotExist Django Error

I am trying to build a login page form. In my urls.py, I have linked the file to the built in Django login view, and pass in the path to a template directory. I have login folder inside the template and login.html file inside the login folder.
(r'^login/$', 'django.contrib.auth.views.login', {
'template_name': 'login/login.html'
}),
In settings.py, I have provided the directory where my templates are being stored.
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__),'templates'),
)
When I run the runserver command, it shows TemplateDoesNotExist
Exception Type: TemplateDoesNotExist
Exception Value:login/login.html
For anyone experiencing the same problem, it turns out the template path was incorrect. I needed to use '../templates', instead of 'templates' in settings.py:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__),'../templates'),
)
A better approach would be define your Directories in another settings file, to alleviate the need to use ugly relative path in your template dirs:
// _paths.py
SETTINGS_DIR = os.path.dirname(__file__)
// _templates.py
from settings/_paths.py import SETTINGS_DIR
TEMPLATE_DIRS = (
os.path.join(SETTINGS_DIR, 'templates'),
)
Adjust accordingly based on your folder structure.

Trouble loading django admin template

I am very new to Django and, following their tutorial, I am having issues loading a custom admin template (specifically, I am having trouble at the bottom of https://docs.djangoproject.com/en/1.4/intro/tutorial02/).
I have updated my TEMPLATE_DIRS so it looks like:
TEMPLATE_DIRS = (
'/Users/myname/Developer/Eclipse/templates/admin',
)
I have also tried:
TEMPLATE_DIRS = (
'/Users/myname/Developer/Eclipse/templates/admin/base_site.html',
)
The path for where I am storing this custom template follows:
"/Users/myname/Developer/Eclipse/templates/admin" (copy and pasted directly from file info)
Could someone point out what I am doing wrong? Thanks in advance.
Unless you don't use your admin folder for all the templates use this (I guess you want to overwrite admin templae. So admin folder should be subfolder of your template folder):
TEMPLATE_DIRS = (
'/Users/myname/Developer/Eclipse/templates/',
)
PROJECT_ROOT = os.path.dirname(__file__)
TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, "templates"),)

TemplateDoesNotExist - file exists, no permissions issue

I'm receiving this error when trying to render a template in django:
TemplateDoesNotExist
...
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:
Here's my settings.py entry:
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'template'),
)
Here's my view that's being called:
def handler(request):
if request.method == 'GET':
NewAppFormSet = modelformset_factory(Application)
return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})
I've tried every possible combination of paths, adjusting permissions, etc. The real issue seems to be with the template loaders, as they are not recognizing any paths set forth in TEMPLATE_DIRS. I've hardcoded this string, to no avail. I've also ran python manage.py runserver with sudo / as root. I'm at a loss...
I had a very similar issue which was driving me crazy. It turns out that the book I was using to learn Python/Django did not say that I had to set the templates directory in settings.py expressly. Very confusing. I am using Python 2.6 & Django 1.3.1 - maybe a previous version automatically found the templates dir. Anyway, after pulling my hair out for a day and reading a lot on this site I figured out that I had to replace the default TEMPLATE_DIRS assignment with the following in settings.py:
# Find templates in the same folder as settings.py.
SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(SETTINGS_PATH, 'templates'),
)
Thanks stackoverflow!!
I'm not sure what example I was following that specified the render_to_response shortcut method requires the views' request object to be passed, but it was as easy as changing it to:
return render_to_response("template/apps.html",{"new_app_form":NewAppFormSet()})
Try assigning SETTINGS_PATH with os.path.realpath:
SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))
By any chance, you might have copy-pasted the TEMPLATE_DIRS you have now, did you leave the default one lower down the SETTINGS file?
From my settings.py:
BASE_DIR = os.path.dirname(__file__)
def RELATIVE_PATH(*args):
return os.path.join(BASE_DIR, *args)
TEMPLATE_DIRS = (
RELATIVE_PATH('template'),
)
what if you try
return render_to_response(request, "apps.html",{"new_app_form":NewAppFormSet})
instead of
return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})