python - 模板不存在 at/polls/index.html

标签 python django

我正在做一个关于 django 项目的教程。 实际上现在是凌晨 4.51,我想让它正常工作。

我的 urls.py 文件:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^polls/$', 'polls.views.index'),
)

我的views.py文件:

from django.shortcuts import render

# Create your views here.
from django.shortcuts import render_to_response


def index(request):
    return render_to_response('index.html')

在文件夹模板中,我有index.html 文件。 它向我展示了相同的 TemplateDoesNotExist 错误,所以我做了一些研究并 I found this question 所以我在我的settings.py中添加了这段代码:

import os.path
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATE_DIRS = (
    os.path.join(SITE_ROOT, 'templates/'),
)

那么如何让它发挥作用呢?

这是回溯:

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/polls/

Django Version: 1.6.1
Python Version: 3.3.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'polls')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\Users\JD\PycharmProjects\MyDjangoApp\MyDjangoApp\templates\index.html

(File does not exist) Using loader django.template.loaders.app_directories.Loader: C:\Python33\lib\site-packages\django\contrib\admin\templates\index.html (File does not exist) C:\Python33\lib\site-packages\django\contrib\auth\templates\index.html (File does not exist)

Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\JD\PycharmProjects\MyDjangoApp\polls\views.py" in index
  8.     return render_to_response('index.html')
File "C:\Python33\lib\site-packages\django\shortcuts\__init__.py" in render_to_response
  29.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "C:\Python33\lib\site-packages\django\template\loader.py" in render_to_string
  162.         t = get_template(template_name)
File "C:\Python33\lib\site-packages\django\template\loader.py" in get_template
  138.     template, origin = find_template(template_name)
File "C:\Python33\lib\site-packages\django\template\loader.py" in find_template
  131.     raise TemplateDoesNotExist(name)

Exception Type: TemplateDoesNotExist at /polls/
Exception Value: index.html

最佳答案

改变

import os.path
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

import os
SITE_ROOT = os.path.dirname(os.path.dirname(__file__))

如果您查看回溯,Python 正在您的应用程序文件夹中查找模板文件夹,我认为它将与 settings.py 位于同一文件夹中。

另一种方法是将模板文件夹复制到 MyDjangoAPP 文件夹中。

关于python - 模板不存在 at/polls/index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21741762/

相关文章:

Python3 `builtins` 并不总是 `__builtins__`

python - python抽象基类是否应该从对象继承

c++ - 将 SWIG 与 C++ 结合使用

python - Django 模型命名约定

Django 属性错误 'datetime.timedelta' 对象没有属性 'decode'

python - 在 python 中使用正则表达式提取日期

python - 表单中的django DateTimeField(初始值)

Django 模型管理器相关过滤器

database - 将 Django 模型从一个应用程序移动到另一个应用程序

python - Django 中的 OneToManyField 关系?