2017-08-11 74 views
0

我在加载Django视图时遇到问题。 FYI很新的Django)Django视图无法从项目文件夹加载

是我得到的错误是TemplateDoesNotExist在/调查/

Django是从 “异常位置 ” 加载/用户/姓名/蟒蛇/ ENVS/Django的/lib/python2.7/site-packages/django/template/loader.py”

应该从加载 “/Users/NAME/Documents/Django/template/index.html”

我在我的APP view.py

return render(request,'index.html') 

我的settings.py

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': ["/Users/NAME/Documents/Django/template/"], 

我如何从我的项目文件的视图加载?

文件结构

total 0 

drwxr-xr-x 6 NAME staff 204 11 Aug 16:17 Django 

drwxr-xr-x 6 NAME staff 204 26 Jun 23:17 test 




./Django: 

total 32 

drwxr-xr-x 10 NAME staff 340 11 Aug 16:17 Django 

-rw-r--r-- 1 NAME staff 12288 10 Aug 21:09 db.sqlite3 

-rwxr-xr-x 1 NAME staff 804 10 Aug 21:08 manage.py 

drwxr-xr-x 15 NAME staff 510 11 Aug 16:17 polls 



./Django/Django: 

total 56 

-rw-r--r-- 1 NAME staff  0 10 Aug 21:08 __init__.py 

-rw-r--r-- 1 NAME staff 147 10 Aug 21:09 __init__.pyc 

-rw-r--r-- 1 NAME staff 3160 11 Aug 10:14 settings.py 

-rw-r--r-- 1 NAME staff 2648 11 Aug 10:14 settings.pyc 

-rw-r--r-- 1 NAME staff 176 10 Aug 21:24 urls.py 

-rw-r--r-- 1 NAME staff 402 10 Aug 21:25 urls.pyc 

-rw-r--r-- 1 NAME staff 390 10 Aug 21:08 wsgi.py 

-rw-r--r-- 1 NAME staff 603 10 Aug 21:09 wsgi.pyc 



./Django/polls: 

total 80 

-rw-r--r-- 1 NAME staff 0 10 Aug 21:19 __init__.py 

-rw-r--r-- 1 NAME staff 146 10 Aug 21:25 __init__.pyc 

-rw-r--r-- 1 NAME staff 128 10 Aug 21:19 admin.py 

-rw-r--r-- 1 NAME staff 150 10 Aug 21:19 apps.py 

drwxr-xr-x 3 NAME staff 102 10 Aug 21:19 migrations 

-rw-r--r-- 1 NAME staff 122 10 Aug 21:19 models.py 

-rw-r--r-- 1 NAME staff 264 10 Aug 21:43 models.pyc 

drwxr-xr-x 3 NAME staff 102 10 Aug 21:40 templates 

-rw-r--r-- 1 NAME staff 125 10 Aug 21:19 tests.py 

-rw-r--r-- 1 NAME staff 116 10 Aug 21:23 urls.py 

-rw-r--r-- 1 NAME staff 326 10 Aug 21:25 urls.pyc 

-rw-r--r-- 1 NAME staff 184 11 Aug 15:56 views.py 

-rw-r--r-- 1 NAME staff 483 11 Aug 15:56 views.pyc 
+0

错误页面通常显示Django试图从中加载模板的文件夹。 –

+0

请显示你的'Django/polls/templates'目录和子目录的布局。 – Alasdair

+0

嘿家伙,对于迟到的回复感到抱歉。周末我在办公室离开了电脑。 克劳斯D.见下文 使用引擎的Django: 'django.template.loaders.app_directories.Loader:/Users/NAME/anaconda/envs/Django/lib/python2.7/site-packages/django/contrib/ admin/templates/index.html(来源不存在)' 'django.template.loaders.app_directories.Loader:/Users/NAME/anaconda/envs/Django/lib/python2.7/site-packages/django/contrib /auth/templates/index.html(来源不存在) ' 麦金太尔 /用户/名/文件/ Django的/ Django的/调查/模板 它只是在它 –

回答

0

Django是仍在寻找旧目录/调查/为模板,可能有几个这种情况的原因。简单解决方案是确保您停止服务器,并重新启动它。在django开发服务器上,当你保存一个文件时,它会做一个软重新加载,然而,对settings.py文件的任何改变都需要完全停止(使用Ctrl + C)并重新启动才能实现。

此外,为确保该目录是正确的,我会建议增加

import os 

在你的文件的开头和

print{os.getcwd()} 

前右你的回报。这样,你可以看到它是在搜索的完整路径,如下面

+0

index.html文件试图重新启动服务器并添加了以下代码 return HttpResponse(os.getcwd()) 这是输出 /Users/NAME/Documents/Django/Django –

+0

你的文件树是如何设置的?您提供的路径使我相信当前目录与您的模板文件夹不在同一个目录,可能会导致问题。一个简单的解决方法是将您的模板文件夹移动到第二个django文件夹中,然后在设置文件中更新模板DIR。 –

+0

刚刚添加文件夹文件结构到主帖 –

0

指定目录:

'DIRS': ["/Users/NAME/Documents/Django/template/"], 

是错误的。您应该使用BASE_DIR的path.join来构建路径。我也不认为你需要指定模板目录,如果你把它留空,那么它也会工作。

'DIRS': [] 

当你离开它空Django会在你的设置app_folder/templates

问题搜索模板是第一您的路径是错误的,你的模板驻留在民意测验目录未Django的目录。其次,您还有一个错字template,而它应该是templates

解决方案:指定本 -

'DIRS': [] 

在您的设置和其他设置和代码都很好。

0

我在设置工作了,

。PY文件是

'DIRS': ["/Users/NAME/Documents/Django/template/"], 

需求是

'DIRS': ['polls/templates'], 

它接缝Django的社区是非常强大的和有益的。这将是我会继续使用Django的主要原因:)

非常感谢大家帮助我。对于nube问题感到抱歉。

+0

也是从这篇文章https://stackoverflow.com/questions/30050634/django-templatedoesnotexist-error-but-using-loader-django-template-loaders-a –

相关问题