2017-04-14 118 views
1

this is the structure of projectDjango的錯誤,TemplateDoesNotExist在/你好

urls.py------ 

    enter code here 

from django.conf.urls import url 
from django.contrib import admin 
from . import search,view 
urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^search-form$', search.search_form), 
    url(r'^search$', search.search), 
    url(r'^test$',search.test), 
    url(r'^hello$',view.hello) 
] 

setting.py------ 
ROOT_URLCONF = 'HelloWorld.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [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', 
      ], 
     }, 
    }, 
] 

------the error------ 

TemplateDoesNotExist at /hello 
hello.html 
Request Method: GET 
Request URL: http://127.0.0.1:8000/hello 
Django Version: 1.7.11 
Exception Type: TemplateDoesNotExist 
Exception Value:  
hello.html 
Exception Location: /Library/Python/2.7/site-packages/Django-1.7.11-py2.7.egg/django/template/loader.py in find_template, line 136 
Python Executable: /usr/bin/python 
Python Version: 2.7.10 
Python Path:  
['/Users/xStraybird/HelloWorld', 
'/Library/Python/2.7/site-packages/Django-1.7.11-py2.7.egg', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', 
'/Library/Python/2.7/site-packages'] 
Server time: Fri, 14 Apr 2017 13:23:10 +0000 



i tried many ways in templates's dirs,such as 

'DIRS':[BASE_DIR + 「/模板」,], 'DIRS': '/用戶/ xStraybird /的HelloWorld /模板', 但它不工作... Django的版本是1.7.11 我不能肯定是路徑錯誤...任何幫助嗎?非常感謝....

+0

創建變量中列出的應用程序模板在settings.py中命名爲'TEMPLATE_PATH',即'TEMPLATE_PATH = os.path.join(BASE_DIR,'templates')'和寫入'DIR:[TEMPLATE_PATH,],' –

+0

仍然錯誤....... TEMPLATE_PATH = os .path.join(BASE_DIR, '模板') TEMPLATES = [{ 'BACKEND':「DJA ngo.template.backends.django.DjangoTemplates', 'DIRS':[TEMPLATE_PATH,], 'APP_DIRS':真, '選項':{ 'context_processors':[ 'django.template.context_processors.debug' , 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] –

+0

看到我的答案在下面.. –

回答

0
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', 
       ], 
      }, 
     }, 
    ] 

這裏我們利用os.path.join()的將BASE_DIR變量和混合在一起3210,其例如將產生<workspace>/HelloWorld/templates/

+0

我知道你的意思,我又累了...仍然錯誤 –

0

我解決問題...在setting.py出現錯誤 - 安裝的應用程序

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'TestModel', 
    'HelloWorld',  # add # 
] 

,因爲當我們使用render,Django會發現這是在INSTALLED_APPS