设置Django模板的位置

时间:2016-04-15 12:31:33

标签: python django templates django-templates

所以我试图通过修改教程来学习Django,顺便说一下,这是版本1.9。

我有我的项目文件夹,名为mud,然后我的app文件夹,民意调查。在民意调查应用程序内部有一个模板文件夹,然后循环回到民意调查文件夹。当我想创建一个模板时,我就把它放在那里。我还想为我的主页面创建一个模板,该模板将在民意调查之外。

我试图在基本MUD文件夹中创建一个模板文件夹,以及mud / mud文件夹,但都不起作用。我总是在寻找index.html of ...

时得到错误

Django尝试按以下顺序加载这些模板:

使用引擎django: django.template.loaders.app_directories.Loader:/home/andaib/themudreport.com/env/lib/python2.7/site-packages/django/contrib/admin/templates/mud/index.html(来源不存在) django.template.loaders.app_directories.Loader:/home/andaib/themudreport.com/env/lib/python2.7/site-packages/django/contrib/auth/templates/mud/index.html(来源不存在) django.template.loaders.app_directories.Loader:/home/andaib/themudreport.com/mud/polls/templates/mud/index.html(来源不存在)

似乎只是在泥浆/民意调查中寻找泥浆/民意调查/泥浆。

这对我来说似乎是疯狂的混乱,我确信必须有一种方法可以添加到我试图访问模板的文件夹列表中,但在我的settings.py中我找不到它。也许有人可以协助?这对于一个对django更有经验的人来说感觉就像是2秒钟。

"""
Django settings for mud project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/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/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=gc!jwa)#q%8t&cl^wi3ago!=dq)ws)xtg5h1s_mgb75q$@uzm'

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

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]


ALLOWED_HOSTS = []


# Application definition

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

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mud.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 = 'mud.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '*****',
        'USER': '*****',
        'PASSWORD': '*****',
        'HOST': '*****',
        'PORT': '*****',
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.9/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/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.dirname(BASE_DIR) + '/public/static/'

2 个答案:

答案 0 :(得分:3)

试试这个:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug': DEBUG,
        },
    },
]

此语法已在Dj v1.9

中弃用
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]

答案 1 :(得分:2)

找到polls文件夹中的模板,因为INSTALLED_APPS位于/mud/templates。 app目录文件夹不会搜索mud/mud/templatemud,因为mud/mud/mud/templates不是应用。

如果您要将模板添加到DIRS,请将其添加到TEMPLATES设置的'DIRS': [os.path.join(BASE_DIR, 'templates')], 选项中。

/mud/mud/templates

同样,如果你想要'DIRS': [os.path.join(BASE_DIR, 'mud', 'templates')], 中的模板,你可以这样做:

 SELECT
    testid,
    testName,
    DATE(testviewaudit.datetime),
    SUM(action = 1) AS view,
    SUM(action = 2) AS start,
    SUM(action = 3) AS finish
  FROM testviewaudit INNER JOIN tmtests ON estviewaudit.testid = tmtests.tsid
  WHERE DATE(testviewaudit.datetime) between '2016-03-12'
	AND  '2016-03-15' GROUP BY testid;