Django TemplateDoesNotExist?
내 로컬 머신은 Ubuntu 8.10에서 Python 2.5 및 Nginx를 실행 중이며 최신 개발 트렁크에서 Django가 빌드되었습니다.
내가 요청하는 모든 URL에 대해 다음을 throw합니다.
/ appname / path appname / template_name.html의 TemplateDoesNotExist
Django는 다음 순서로 이러한 템플릿을로드하려고 시도했습니다. * 로더 django.template.loaders.filesystem.function 사용 : * 로더 django.template.loaders.app_directories.function 사용 :
TEMPLATE_DIRS ( '/usr/lib/python2.5/site-packages/projectname/templates',)
이 경우 /usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html 을 찾고 있습니까? 이상한 것은이 파일이 디스크에 존재한다는 것입니다. Django가 왜 그것을 찾을 수 없습니까?
그런 문제없이 Ubuntu 9.04의 Python 2.6을 사용하여 원격 서버에서 동일한 응용 프로그램을 실행합니다. 다른 설정은 동일합니다.
로컬 컴퓨터에 잘못 구성된 것이 있습니까, 아니면 내가 조사해야 할 오류를 일으킬 수있는 것은 무엇입니까?
내에서 settings.py , 내가 지정한 :
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
# Find templates in the same folder as settings.py.
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
다음 파일을 찾아야합니다.
- /usr/lib/python2.5/site-packages/projectname/templates/appname1/template1.html
- /usr/lib/python2.5/site-packages/projectname/templates/appname1/template2.html
- /usr/lib/python2.5/site-packages/projectname/templates/appname2/template3.html
- ...
위의 모든 파일은 디스크에 존재합니다.
해결
내가 시도한 후에 지금 작동합니다.
chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*
이상하다. 원격 서버 에서이 작업을 수행 할 필요가 없습니다.
첫 번째 해결책 :
이 설정
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
Django는 templates/
프로젝트의 디렉토리에서 템플릿을 볼 것임을 의미합니다 .
장고 프로젝트가 /usr/lib/python2.5/site-packages/projectname/
설정 에 있다고 가정하면 django는 아래에서 템플릿을 찾습니다./usr/lib/python2.5/site-packages/projectname/templates/
이 경우 템플릿을 다음과 같이 구성하도록 옮깁니다.
/usr/lib/python2.5/site-packages/projectname/templates/template1.html
/usr/lib/python2.5/site-packages/projectname/templates/template2.html
/usr/lib/python2.5/site-packages/projectname/templates/template3.html
두 번째 해결책 :
그래도 작동하지 않고 settings.py에 앱을 구성했다고 가정하면 다음과 같습니다.
INSTALLED_APPS = (
'appname1',
'appname2',
'appname3',
)
Django는 기본적으로 templates/
설치된 모든 앱의 디렉토리 아래에 템플릿을로드합니다 . 따라서 디렉토리 구조를 사용하여 템플릿을 다음과 같이 이동하려고합니다.
/usr/lib/python2.5/site-packages/projectname/appname1/templates/template1.html
/usr/lib/python2.5/site-packages/projectname/appname2/templates/template2.html
/usr/lib/python2.5/site-packages/projectname/appname3/templates/template3.html
희망이 도움이됩니다.
SETTINGS_PATH
기본적으로 정의되어 있지 않을 수 있습니다. 이 경우 settings.py에서 정의하고 싶을 것입니다.
import os
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
이 튜플을 찾으십시오.
import os
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
문자열을 'DIRS'에 추가해야합니다.
"os.path.join(SETTINGS_PATH, 'templates')"
그래서 당신은 모두 필요합니다 :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(SETTINGS_PATH, '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',
],
},
},
]
.py 설정에서 TEMPLATE_LOADERS 및 TEMPLATE DIRS를 제거한 다음 추가
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/jay/apijay/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',
],
},
},
]
If you encounter this problem when you add an app
from scratch. It is probably because that you miss some settings
. Three steps is needed when adding an app
.
1、Create the directory and template file.
Suppose you have a project named mysite
and you want to add an app
named your_app_name
. Put your template file under mysite/your_app_name/templates/your_app_name
as following.
├── mysite
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── your_app_name
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── templates
│ │ └── your_app_name
│ │ └── my_index.html
│ ├── urls.py
│ └── views.py
2、Add your app
to INSTALLED_APPS
.
Modify settings.py
INSTALLED_APPS = [
...
'your_app_name',
...
]
3、Add your app
directory to DIRS
in TEMPLATES
.
Modify settings.py
.
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'your_app_name', 'templates', 'your_app_name'),
...
]
}
]
Just a hunch, but check out this article on Django template loading. In particular, make sure you have django.template.loaders.app_directories.Loader
in your TEMPLATE_LOADERS list.
Check permissions on templates and appname directories, either with ls -l or try doing an absolute path open() from django.
It works now after I tried
chown -R www-data:www-data /usr/lib/python2.5/site-packages/projectname/*
It's strange. I dont need to do this on the remote server to make it work.
Also, I have to run the following command on local machine to make all static files accessable but on remote server they are all "root:root".
chown -R www-data:www-data /var/www/projectname/*
Local machine runs on Ubuntu 8.04 desktop edition. Remote server is on Ubuntu 9.04 server edition.
Anybody knows why?
I had an embarrassing problem...
I got this error because I was rushing and forgot to put the app in INSTALLED_APPS
. You would think Django would raise a more descriptive error.
For the django version 1.9,I added
'DIRS': [os.path.join(BASE_DIR, 'templates')],
line to the Templates block in settings.py And it worked well
Django TemplateDoesNotExist
error means simply that the framework can't find the template file.
To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file (settings.py
) by TEMPLATE_DIRS
setting. By default it's an empty tuple, so this setting tells Django's template-loading mechanism where to look for templates.
Pick a directory where you'd like to store your templates and add it to TEMPLATE_DIRS e.g.:
TEMPLATE_DIRS = (
'/home/django/myproject/templates',
)
Check that your templates.html are in /usr/lib/python2.5/site-packages/projectname/templates
dir.
See which folder django try to load template look at Template-loader postmortem
in error page, for example, error will sothing like this:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: d:\projects\vcsrc\vcsrc\templates\base.html (Source does not exist)
In my error vcsrc\vcsrc\templates\base.html
not in path.
Then change TEMPLATES
in setting.py
file to your templates path
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'vcsrc/templates')],
...
Hi guys I found a new solution. Actually it is defined in another template so instead of defining TEMPLATE_DIRS yourself, put your directory path name at their:
I must use templates for a internal APP and it works for me:
'DIRS': [os.path.join(BASE_DIR + '/THE_APP_NAME', 'templates')],
I'm embarrassed to admit this, but the problem for me was that a template had been specified as ….hml
instead of ….html
. Watch out!
I added this
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
and it still showed the error, then I realized that in another project the templates was showing without adding that code in settings.py file so I checked that project and I realized that I didn't create a virtual environment in this project so I did
virtualenv env
and it worked, don't know why
I came up with this problem. Here is how I solved this:
Look at your settings.py, locate to TEMPLATES
variable, inside the TEMPLATES, add your templates path inside the DIRS
list. For me, first I set my templates path as TEMPLATES_PATH = os.path.join(BASE_DIR,'templates')
, then add TEMPLATES_PATH
into DIRS
list, 'DIRS':[TEMPLATES_PATH,]
. Then restart the server, the TemplateDoesNotExist exception is gone. That's it.
in your setting.py
file replace DIRS
in TEMPLATES
array with this
'DIRS': []
to this
'DIRS': [os.path.join(BASE_DIR, 'templates')],
but 1 think u need to know is that you have to make a folder with name templates
and it should on the root path otherwise u have to change the DIRS
value
1.create a folder 'templates' in your 'app'(let say you named such your app) and you can put the html file here. But it s strongly recommended to create a folder with same name('app') in 'templates' folder and only then put htmls there. Into the 'app/templates/app' folder
2.now in 'app' 's urls.py put:
path('', views.index, name='index'), # in case of use default server index.html
3. in 'app' 's views.py put:
from django.shortcuts import render
def index(request): return
render(request,"app/index.html")
# name 'index' as you want
참고URL : https://stackoverflow.com/questions/1926049/django-templatedoesnotexist
'Programing' 카테고리의 다른 글
Django Rest Framework를 사용하여 관련 모델 필드를 어떻게 포함합니까? (0) | 2020.06.25 |
---|---|
그룹 객체에 변형 대 적용 (0) | 2020.06.25 |
JDBC와 함께 try-with-resources를 어떻게 사용해야합니까? (0) | 2020.06.25 |
Bash를 사용하여 마지막 명령의 출력을 변수로 자동 캡처합니까? (0) | 2020.06.25 |
실제로 오버로드 된 && 및 || 이유가 있습니까? (0) | 2020.06.25 |