What is the path that Django uses for locating and loading templates?
I know this isn't in the Django tutorial, and shame on them, but it's better to set up relative paths for your path variables. You can set it up like so:
import os.path
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
...
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media/')
TEMPLATE_DIRS = [
os.path.join(PROJECT_PATH, 'templates/'),
]
This way you can move your Django project and your path roots will update automatically. This is useful when you're setting up your production server.
Second, there's something suspect to your TEMPLATE_DIRS path. It should point to the root of your template directory. Also, it should also end in a trailing /
.
I'm just going to guess here that the .../admin/
directory is not your template root. If you still want to write absolute paths you should take out the reference to the admin template directory.
TEMPLATE_DIRS = [
'C:/django-project/myapp/mytemplates/',
]
With that being said, the template loaders by default should be set up to recursively traverse into your app directories to locate template files.
TEMPLATE_LOADERS = [
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
]
You shouldn't need to copy over the admin templates unless if you specifically want to overwrite something.
You will have to run a syncdb if you haven't run it yet. You'll also need to statically server your media files if you're hosting django through runserver.
If using Django settings as installed, then why not just use its baked-in, predefined BASE_DIR and TEMPLATES? In the pip installed Django(v1.8), I get:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
### ADD YOUR DIRECTORY HERE LIKE SO:
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',
],
},
},
]
Smart solution in Django 2.0.3 for keeping templates in project directory (/root/templates/app_name
):
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMP_DIR = os.path.join(BASE_DIR, 'templates')
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMP_DIR],
...
in views.py just add such template path:
app_name/html_name
For Django 1.6.6:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = os.path.join(BASE_DIR, 'templates')
Also static and media for debug and production mode:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
if DEBUG:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
else:
STATIC_ROOT = %REAL_PATH_TO_PRODUCTION_STATIC_FOLDER%
MEDIA_ROOT = %REAL_PATH_TO_PRODUCTION_MEDIA_FOLDER%
Into urls.py you must add:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from news.views import Index
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
...
)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In Django 1.8 you can set template paths, backend and other parameters for templates in one dictionary (settings.py):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
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',
],
},
},
]
Official docs.