我跟着這個Django教程:https://docs.djangoproject.com/en/1.10/intro/reusable-apps/。 我有一個名爲oldcity
的項目和一個名爲oldantwerp
的應用程序。該應用位於名爲django-oldantwerp
的父目錄中,應用目錄本身有一個子目錄templates
。該index.html
文件,我的項目正在尋找位於象這樣:使用Django包時的TemplateDoesNotExist
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html
我試圖將其包含在settings.py使用此應用與我的項目:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'oldantwerp'
]
和urls.py (在工程中的),像這樣:
urlpatterns = [
url(r'^oldantwerp/', include('oldantwerp.urls')),
url(r'^admin/', admin.site.urls),
]
當我進入管理頁面,一切正常,但是當我嘗試打開索引頁我得到這個錯誤:
TemplateDoesNotExist at /oldantwerp/
它說,它試圖找到index.html文件,如下所示:
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/admin/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/auth/templates/oldantwerp/index.html (Source does not exist)
而且它也試圖尋找另一個文件:place_list.html
,這很奇怪,因爲我不認爲我有這樣的一份文件。
什麼可能是錯的?
編輯
這是views.py在oldantwerp文件夾中的代碼:
class IndexView(generic.ListView):
template_name = 'oldantwerp/index.html'
context_object_name = 'places'
def get_queryset(self):
return Place.objects.order_by('id')[:]
編輯
也許值得一提:這一切沒有工作的時候我剛做了文件夾oldantwerp
作爲oldicty
項目文件夾中的子目錄。這個錯誤只發生在我從一個外部包開始實現它之後。
編輯
這是我的模板設置在settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
請告訴我們您的看法... –
我編輯它在那裏。 – Vincent
您是否記得在[教程](https://docs.djangoproject.com/en/1.10/intro/reusable-apps/)中的清單文件中包含模板(步驟6)?如果你輸入'import oldantwerp;打印(oldantwerp .__文件__)'到你的代碼,它顯示的路徑是什麼? – Alasdair