2017-02-07 21 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:

<html><body>It is now {{ current_date }}.</body></html> 

そして、私はこれだ:それを解決するためにどのように

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と同じパスレベルであることにより、得ABSパスと同じだかどうかを確認することができ、それ次のようになります。内側 `mysite`のディレクトリにあるテンプレートのディレクトリを期待しているよう os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

関連する問題