2017-02-10 84 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吗? –