【发布时间】:2015-11-21 23:22:19
【问题描述】:
我在 Ubuntu 上开发一个基本的 Web 应用程序。我的项目目录结构如下所示:
tango
-rango
-migrations
-tango
-templates
-rango
-index.html
/tango/urls.py
from django.conf.urls import include, url, patterns
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tango_with_django_project_17.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)
rango/urls.py
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'))
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
TEMPLATE_PATH,
)
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
context_dict = {'boldmessage': "I am bold font from the context"}
return render(request, 'rango/index.html', context_dict)
index.html
<!DOCTYPE html>
<html>
<head>
<title>Rango</title>
</head>
<body>
<h1>Rango says...</h1>
hello world! <strong>{{ boldmessage }}</strong><br />
<a href="/rango/about/">About</a><br />
</body>
</html>
但是,当我尝试访问 http://localhost:8000/rango/ 时,这是我遇到的错误:
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/manas/D_Drive/Django/tango/rango/views.py" in index
14. return render(request, 'rango/index.html', context_dict)
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
98. template = get_template(template_name, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in get_template
46. raise TemplateDoesNotExist(template_name)
Exception Type: TemplateDoesNotExist at /rango/
Exception Value: rango/index.html
index.html存储在以下路径:/home/manas/D_Drive/Django/tango/templates/rango/index.html
我正在使用 Django 1.8 和 Python 2.7 并在 Ubunty 14.04 上运行应用程序。
这里似乎有什么问题?
编辑:
模板设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
【问题讨论】:
-
您尚未显示您的模板设置。请注意,Tango with Django 是为 Django 1.7 编写的,但 Django 1.8 有一个新的
TEMPLATES设置。应该可以使代码与 1.8 一起使用,但您可能会发现使用 Django 1.7 更容易。 -
@Alasdair 我已经编辑了我的原始帖子以包含模板设置。
-
尝试将您的模板路径添加到目录,即
DIRS: [TEMPLATE_PATH],
标签: python django python-2.7 python-3.x django-templates