On my site i have demo
template in specific folder:
project_root
├── project
│ ├── design # templates
│ │ ├── demo # template folder
│ │ │ └── index.html # template file
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── some_app
│ └── ...
└── manage.py
I have tried to use TemplateView
to render my demo/index.html
from specific url but Django fails to find my template, even if i add /project/design
to TEMPLATE_DIRS
or TEMPLATES
.
I need to render template using demo/index.html
string having templates in /project/design
folder. How do i do that?
Add in the right template engine that you use, for example DjangoTemplate , this directory.
Eg ( settings.py
file) :
from unipath import Path
BASE_DIR = Path(__file__).ancestor(2)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR + '/project/design/',
],
'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',
],
},
},
]
Check that BASE_DIR
variable corresponds to the folder that contains all your apps.
After that,rend your template with :
return render(request, 'demo/index.html')
Best practice : Put your templates in project_root/templates/<app_name>/
or in templates/<app_name>
folder of each apps.
Read this for more information about templates : Django - Templates
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.