gpt4 book ai didi

python - 模板不存在 : Wrong template path

转载 作者:行者123 更新时间:2023-11-30 23:07:24 25 4
gpt4 key购买 nike

我在 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'))

设置.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,

)

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

最佳答案

您的 TEMPLATES 配置中的 'DIRS' 列表为空,因此它无法找到您的模板文件夹。只需将其替换为: 'DIRS': [os.path.join(BASE_DIR, 'templates')]'DIRS': [TEMPLATE_PATH] 就可以了,

关于python - 模板不存在 : Wrong template path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251712/

25 4 0