Django 2.0可重用应用程序 - 在/ polls /上的TemplateDoesNotExist

时间:2017-12-19 19:58:47

标签: django templates

Python 3,Django 2.0

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

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

此处存在的问题是:该文件已被命名为manifest.in,当需要将其大写为MANIFEST.in时。节日快乐!