【问题标题】:Django 2.0 reusable apps - TemplateDoesNotExist at /polls/Django 2.0 可重用应用程序 - TemplateDoesNotExist at /polls/
【发布时间】:2018-06-02 07:44:46
【问题描述】:

Python 3、Django 2.0

我刚刚结束了官方 Django 教程,我正在学习高级部分 - “如何编写可重用的应用程序”

似乎我至少部分成功了,我可以通过管理界面访问问题和选择,但是当我尝试显示 http://127.0.0.1:8000/polls/ 时出现此错误:TemplateDoesNotExist at /polls/1/

我在尝试访问详细信息页面时遇到了类似的错误,http://127.0.0.1:8000/polls/1/

我查看了所有可以解决的问题,我的主要问题是 - 如何访问打包到应用程序中的模板?我看到的错误是 Django 试图在主项目目录中找到应用程序模板(即:/tutorial/templates/polls/detail.html),但它们应该在我从 pip 导入的模块“内部”。

您真的应该以这种方式将模板打包到应用程序中吗?我尝试将模板文件放回 tutorial/templates/polls/ 并且它工作得很好,但这违背了我认为应该是“可重用性”的内容,因为打包的应用程序将没有它自己的模板。

我能找到的答案似乎更多地适用于旧版本的 Django,并使用了 TEMPLATE_LOADERS 设置...有人知道是否有办法在 Django 2 中进行设置?

目录结构:

django-polls/
  LICENSE
  manifest.in
  README.rst
  setup.py
  /dist
  /docs
  /polls
    admin.py
    apps.pyo
    __init__.py
    models.py
    tests.py
    urls.py
    views.py
    /build
    /migrations
    /static
      /polls
        /images
      style.css
    /templates
      /polls
        detail.html
        index.html
        results.html

项目结构是这样的:

tutorial/
  /mysite  (settings.py, etc)
  /templates
    /admin
      base_site.html
      index.html
  /venv (my virtualenv directory)
  db.sqlite3
  manage.py

settings.py

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 2.0.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*e7@6=f$r(cu_p@7#*s+6t9r^ouio$x&06s61-0u(n1mo370c6'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    # 'polls.apps.PollsConfig',
    'polls',
    '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 = 'mysite.urls'

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

WSGI_APPLICATION = 'mysite.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'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Chicago'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

【问题讨论】:

  • 请包含 Django 搜索的目录列表。

标签: django templates


【解决方案1】:

这里的问题是大小写:文件被命名为manifest.in,而它需要大写为MANIFEST.in。节日快乐!

【讨论】:

    【解决方案2】:

    请记住将您的templates 目录包含在您的MANIFEST.in 文件中。有关详细信息,请参阅packaging your app 的第 6 步。

    【讨论】:

    • 我不是 100% 确定这是否需要,但也要注意大小写。我一直看到文件大小写为MANIFEST.in。 OP 在他的树中将其命名为manifest.in,全部小写。
    • 哇,你知道了,非常感谢!我已经在清单中打包了模板目录,但这是小写的问题。
    • 好地方@FlipperPA
    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 2019-08-21
    相关资源
    最近更新 更多