上的Django TemplateDoesNotExist錯誤我一直在下面的教程Django的Tango with DjangoWindows機器
我試圖添加模板上link的指示。 我在Windows 7機器上使用Python 2.7,Django 1.8。
下面是錯誤,我得到:
TemplateDoesNotExist at /rango/
rango/index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/rango/
Django Version: 1.8
Exception Type: TemplateDoesNotExist
Exception Value:rango/index.html
Exception Location: C:\Python27\lib\site-packages\django\template\loader.py in get_template, line 46
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\rango\index.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\rango\index.html (File does not exist)
下面是我的文件結構:
tango_with_django_project
+-- rango
| +--views.py
| +--other files
+-- tango_with_django_project
| +--templates
| | +--rango
| | | +--index.html
| +--settings.py
| +--other files
+-- db.sqlite3
+-- manage.py`
我在settings.py
給出的模板路徑如下:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR,'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
,並且已經設置視圖爲views.py
:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
context_dict = {'boldmessage': "I am bold font from the context"}
return render(request, 'rango/index.html', context_dict)
如果不使用模板,它工作正常,並顯示純文本。但是,當我做模板所需的更改時,它不起作用並引發錯誤。 在同一個問題上有很多鏈接,但沒有人爲我工作。 與one幾乎相同的錯誤描述和在Windows機器上有一個模糊的答案。
我已經嘗試過直接指定絕對路徑而不是從os.path
得到它,並且還嘗試將模板文件夾放在不同的路徑中。
任何幫助,將不勝感激。
Django沒有看你的'TEMPLATE_DIRS'。你確定你不重寫這個變量嗎?在你看來,嘗試使用'from django。conf導入設置; print(settings.TEMPLATE_DIRS)' –
@AndreaCorbellini:即使我認爲'TEMPLATE_DIRS'沒有設置我指定的值。它檢查這些路徑中的模板:C:\ Python27 \ lib \ site-packages \ django \ contrib \ admin; C:\ Python27 \ lib \ site-packages \ django \ contrib \ auth' 我試過你告訴我的並得到以下錯誤: 'django.core.exceptions.ImproperlyConfigured:請求的設置TEMPLATE_DIRS,但設置未配置。您必須先定義環境變量DJANGO_SETTINGS_MODULE,或者在訪問設置之前調用settings.configure()。# – Vaulstein
您得到的錯誤非常自我解釋。你設置了'DJANGO_SETTINGS_MODULE'還是你調用了'settings.configure()'?你的web應用程序如何運行? (manage.py,一個web服務器,...) –