its-django / mysite

<<It's Django - 用Python迅速打造Web應用>> 書中程式碼
Apache License 2.0
48 stars 25 forks source link

我也碰到TemplateDoesNotExist #19

Open felix0221 opened 7 years ago

felix0221 commented 7 years ago

錯誤如下

TemplateDoesNotExist at / index.html Request Method: GET Request URL: http://120.97.63.249:8000/ Django Version: 1.10.6 Exception Type: TemplateDoesNotExist Exception Value:
index.html Exception Location: /home/felix/healthENV/local/lib/python2.7/site-packages/django/template/loader.py in get_template, line 25 Python Executable: /home/felix/healthENV/bin/python Python Version: 2.7.6 Python Path:
['/home/felix/healthclub', '/home/felix/healthENV/lib/python2.7', '/home/felix/healthENV/lib/python2.7/plat-x86_64-linux-gnu', '/home/felix/healthENV/lib/python2.7/lib-tk', '/home/felix/healthENV/lib/python2.7/lib-old', '/home/felix/healthENV/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/home/felix/healthENV/local/lib/python2.7/site-packages', '/home/felix/healthENV/lib/python2.7/site-packages'] Server time: 星期日, 5 三月 2017 11:35:48 +0800 Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django: django.template.loaders.filesystem.Loader: /home/felix/healthclub/templates/index.html (Source does not exist) django.template.loaders.app_directories.Loader: /home/felix/healthENV/local/lib/python2.7/site-packages/django/contrib/admin/templates/index.html (Source does not exist) django.template.loaders.app_directories.Loader: /home/felix/healthENV/local/lib/python2.7/site-packages/django/contrib/auth/templates/index.html (Source does not exist) django.template.loaders.app_directories.Loader: /home/felix/healthENV/local/lib/python2.7/site-packages/ckeditor/templates/index.html (Source does not exist) django.template.loaders.app_directories.Loader: /home/felix/healthENV/local/lib/python2.7/site-packages/ckeditor_uploader/templates/index.html (Source does not exist) django.template.loaders.app_directories.Loader: /home/felix/healthENV/local/lib/python2.7/site-packages/xadmin/templates/index.html (Source does not exist) django.template.loaders.app_directories.Loader: /home/felix/healthENV/local/lib/python2.7/site-packages/crispy_forms/templates/index.html (Source does not exist) django.template.loaders.app_directories.Loader: /home/felix/healthENV/local/lib/python2.7/site-packages/reversion/templates/index.html (Source does not exist)

但檔案確實存在於/home/felix/healthclub/templates/index.htm

麻煩大大指導了,謝謝

myyang commented 7 years ago

@felix0221

那麼有可能是你的 TEMPLATES 設定沒有加入你的templates資料夾 你可以研究一下你的exception: Django tried loading these templates, in this order 以下的資訊,他告訴你django的engine找了哪些資料夾,目前看起來都是virtualnev下的資料夾,沒有找你的project

如果你的project是healthclub,那麼加入dirs設定:

TEMPLATES = [
    {
     .....
     'DIRS': ['healthclub/templates',],
     .....
]

dirs中的路徑可以用os.path.join動態產生絕對的路徑,可能更有可靠度和彈性