python - django2 + python3 : TemplateDoesNotExist

标签 python django python-3.x django-templates

我知道这个网站上有很多关于这个问题的问题,但我找不到解决方案。

我在 Windows 10 上使用 Python 3.6 (anaconda) + django 2.0.2。

我正在学习教程:https://docs.djangoproject.com/en/2.0/intro/tutorial03/

这是我的views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

from .models import *


def index(request):
    content = 'abcxyz'

    context = {'content': content}
    return render(request, 'polls/index.html', context)

我在文件夹 polls\templates\polls 中创建了一个文件 index.html

我的settings.py:

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
]

ROOT_URLCONF = 'django_site.urls'

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

WSGI_APPLICATION = 'django_site.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

我有一个“TemplateDoesNotExist”的问题——在我看来 django 试图在

django.template.loaders.app_directories.Loader: /mnt/e/workspace/capec-processing/code/django_site/polls/templates/polls/templates/polls/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user_name/anaconda3/envs/capec/lib/python3.6/site-packages/django/contrib/admin/templates/polls/templates/polls/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user_name/anaconda3/envs/capec/lib/python3.6/site-packages/django/contrib/auth/templates/polls/templates/polls/index.html (Source does not exist)

我不确定我做错了什么,因为我按照django网站上的教程。

你能给我一个提示吗?

更新

这是我的根目录(称为django_site)的结构:

django_site
--django_site
----settings.py
--polls
----templates
------polls
--------index.html
----views.py
--db.sqlite3
--manage.py

最佳答案

在你的setting.py中你需要添加这个

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls'   # You need to add this too. This should be same as your app name.
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],  # Add this to your settings file
        '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',
            ],
        },
    },
]

你的模板文件夹应该在这里

myproject/
 |-- myproject/
 |    |-- polls/
 |    |-- myproject/
 |    |-- templates/     <-- here!
 |    |    |-- polls/
 |    |    |    |-- index.html
 |    |    |-- base.html
 |    |    +-- home.html
 |    +-- manage.py
 +-- venv/

关于python - django2 + python3 : TemplateDoesNotExist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804194/

相关文章:

python - Numpy 数组索引与其他数组会产生广播错误

python - Django 分组依据

django - 使用 update_fields 保存不会影响任何行(更改类时)

python - 使用 Python 和 BeautifulSoup 抓取 Amazon 数据时出错

python - 如何安全地将整数矩阵预分配为 numpy 中的索引矩阵

python - 视频中的直方图均衡

python - 在 Google AppEngine 上使用 upload_data 不允许我使用基于 id 的键更新实体

python-3.x - 检查字符串是否是字符串列表中的子字符串的最快方法

python - 随机数文件写入和读取

python - 如何使用 NLTK ne_chunk 提取 GPE(位置)?