简体   繁体   English

Django错误,在/ hello的TemplateDoesNotExist

[英]Django error,TemplateDoesNotExist at /hello

this is the structure of project 这是项目的结构

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+"/templates",], 'DIRS': '/Users/xStraybird/HelloWorld/templates', but it doesn't work...django version is 1.7.11 i can't sure that is path error...any help?thanks a lot.... 'DIRS':[BASE_DIR +“/ templates”,],'DIRS':'/ Users / xStraybird / HelloWorld / templates',但它不起作用... django版本是1.7.11我不能确定是路径错误......有什么帮助吗?非常感谢....

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

Here we make use of the os.path.join() to mash together the BASE_DIR variable and 'templates' , which would for example yield <workspace>/HelloWorld/templates/ . 这里我们使用os.path.join()BASE_DIR变量和'templates'混合在一起,例如产生<workspace>/HelloWorld/templates/

i solve the question ...error occurs in setting.py--installed apps 我解决了这个问题...在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  #
]

because when we use render ,django will find templates in apps which is listed in INSTALLED_APPS 因为当我们使用render ,django会在INSTALLED_APPS列出的应用中找到模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM