add template in django code example
Example 1: dirs' base_dir / 'templates' error
Incorrect syntax in polls app tutorial by Django
Use this:
os.path.join(BASE_DIR, 'templates')
instead of this:
BASE_DIR / 'templates'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
],
},
},
]
Example 2: how to connect templates in django
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
Example 3: how to get all template project in settinge use django
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
class InvalidTemplateVariable(str):
def __mod__(self,other):
from django.template.base import TemplateSyntaxError
raise TemplateSyntaxError("Invalid variable : '%s'" % other)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['%s/templates/' % (PROJECT_DIR),'%s/dev_templates/' % (PROJECT_DIR),],
'APP_DIRS': True,
'OPTIONS': {
'string_if_invalid': InvalidTemplateVariable("%s"),
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]