2017-02-10 83 views
0

我跟着這個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', 
     ], 
    }, 
}, 
] 
+1

請告訴我們您的看法... –

+0

我編輯它在那裏。 – Vincent

+0

您是否記得在[教程](https://docs.djangoproject.com/en/1.10/intro/reusable-apps/)中的清單文件中包含模板(步驟6)?如果你輸入'import oldantwerp;打印(oldantwerp .__文件__)'到你的代碼,它顯示的路徑是什麼? – Alasdair

回答

0

一個最可能的問題可能是你已經拿到了模板名稱錯誤在您的視圖。確保您在視圖中使用模板oldantwerp/index.html,而不僅僅是index.html

+0

我編輯了這個問題,我的確在使用oldantwerp/index.html – Vincent

0

如果你看看你的堆棧跟蹤,你會看到你的應用程序試圖從加載模板:如果我看了你的第一條語句正確的目錄結構* django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist).

django-oldantwerp>oldantwerp>templates>oldantwerp>index.html. 

所以,你可以創建一個目錄結構適合django來識別你的模板目錄,或者你可以從設置文件中更新你的模板常量,並將django指向它可以找到你的模板的物理位置。

+0

這兩個路徑的區別在於第一個嘗試查找索引。在dec市(項目)文件夾的html,而實際的模板文件夾位於django-oldantwerp包。我不認爲這是一個好主意,存儲在項目文件夾本身的應用程序模板... – Vincent

0

您可以嘗試在settings.py中設置DIRS,如下所示:os.path。加入(BASE_DIR,'模板'),從模板文件夾中刪除oldantwerp文件夾,並在模板中添加index.html。

之後,編輯TEMPLATE_NAME = 'oldantwerp/index.html的' 以TEMPLATE_NAME = '的index.html'

+0

我試過這個但它沒有工作.. – Vincent

+0

是APP_DIRS設置爲True? –

+0

你可以做更多的事情,你使用Pycharm嗎? –