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', 但它不起作用...... django版本是1.7.11 我不能确定是路径错误......有什么帮助吗?非常感谢....
答案 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
变量和'templates'
混合在一起,例如会产生<workspace>/HelloWorld/templates/
。
答案 1 :(得分: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