2017-02-07 1 views
0

enter image description hereDjango에서 TemplateDoesNotExist를 푸는 방법?

설정 :

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),], 
     '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', 
      ], 
     }, 
    }, 
] 

URL을 :

urlpatterns = [ 
    url(r'^hello/$', hello), 
    url(r'^time/$', current_datetime), 
    url(r'^time/plus/(\d{1,2})/$', hours_ahead), 
] 

전망 :

def current_datetime(request): 
    now = datetime.datetime.now() 
    t = get_template('current_datetime.html') 
    html = t.render(Context({'current_date': now})) 
    return HttpResponse(html) 

current_datetime.html :

,345,728,913,378,603,210

그리고이있어 :

TemplateDoesNotExist at /time/ 
current_datetime.html 
Request Method: GET 
Request URL: http://127.0.0.1:8000/time/ 
Django Version: 1.10.5 
Exception Type: TemplateDoesNotExist 
Exception Value:  
current_datetime.html 
Exception Location: /Users/wonderful/anaconda/lib/python2.7/site-packages/django/template/loader.py in get_template, line 25 
Python Executable: /Users/wonderful/anaconda/bin/python 
Python Version: 2.7.12 
Python Path:  

    ['/Users/wonderful/mysite', 
    '/Users/wonderful/anaconda/lib/python27.zip', 
    '/Users/wonderful/anaconda/lib/python2.7', 
    '/Users/wonderful/anaconda/lib/python2.7/plat-darwin', 
    '/Users/wonderful/anaconda/lib/python2.7/plat-mac', 
    '/Users/wonderful/anaconda/lib/python2.7/plat-mac/lib-scriptpackages', 
    '/Users/wonderful/anaconda/lib/python2.7/lib-tk', 
    '/Users/wonderful/anaconda/lib/python2.7/lib-old', 
    '/Users/wonderful/anaconda/lib/python2.7/lib-dynload', 
    '/Users/wonderful/anaconda/lib/python2.7/site-packages', 
    '/Users/wonderful/anaconda/lib/python2.7/site-packages/Sphinx-1.4.6-py2.7.egg', 
    '/Users/wonderful/anaconda/lib/python2.7/site-packages/aeosa', 
    '/Users/wonderful/anaconda/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg'] 

Server time: Tue, 7 Feb 2017 10:42:42 +0800 

Template-loader postmortem 

Django tried loading these templates, in this order: 

Using engine django: 
django.template.loaders.filesystem.Loader: /Users/wonderful/mysite/mysite/templates/current_datetime.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/wonderful/anaconda/lib/python2.7/site-packages/django/contrib/admin/templates/current_datetime.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/wonderful/anaconda/lib/python2.7/site-packages/django/contrib/auth/templates/current_datetime.html (Source does not exist) 

그 해결 방법을?

+1

보이는 – karthikr

+0

@karthikr 네, 그것은 귀하의 폴더가 "템플릿"라는 – ipreacher

+0

및 설정에서 당신이 그것을 설명했다 "템플릿을 작동 " – Bijoy

답변

0

정확한 템플릿 절대 경로를 제공하지 않은 것 같습니다.

템플릿 폴더로 이동하여 pwd을 터미널에 실행하면 절대 경로가 표시되고 현재 DIRS으로 바뀌고 작동하는지 확인하십시오.

당신은 당신의 os.path.dirname (__ FILE__)을 인쇄하여 내 이해, 당신은 pwd

templates 디렉토리 mysite과 같은 경로 수준에 의해 얻은 복근 경로와 일치 여부 확인할 수 있습니다 그것은 해야합니다 : 내부`mysite` 디렉토리에 있어야 템플릿 디렉토리를 기대하고있다 os.path.dirname(os.path.dirname(os.path.abspath(__file__)))