简体   繁体   English

Django未加载模板,但模板位置出错

[英]Django not loading templates, but gives error with template location

Okay, I am using django 1.8. 好的,我正在使用django 1.8。 I am trying to get templates working with render in my views. 我试图在我的视图中使用可渲染的模板。 Here's my layout: 这是我的布局:

├── db.sqlite3
├── django_classr_test
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── manage.py
├── static_pages
│   ├── admin.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-34.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-34.pyc
│   │   ├── __init__.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── templates
│   │   └── static_pages
│   │       ├── contact.html
│   │       ├── index.html
│   │       └── legal.html
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc

So I have a templates folder inside my static_pages app folder. 所以我的static_pages应用程序文件夹中有一个模板文件夹。 So, I should just be able to say render(request, 'static_pages/index.html') , right? 所以,我应该只能说render(request, 'static_pages/index.html') ,对吗? Here's my view: 这是我的看法:

from django.shortcuts import render

from django.http import HttpResponse
def index(request):
    return render(request, 'static_pages/templates/static_pages/index.html')

And yet, it gives this error when I visit the page: 但是,当我访问页面时,它给出了此错误:

TemplateDoesNotExist at /

static_pages/templates/static_pages/index.html

Request Method:     GET
Request URL:    http://localhost:8000/
Django Version:     1.8.3
Exception Type:     TemplateDoesNotExist
Exception Value:    

static_pages/templates/static_pages/index.html

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/template/loader.py in get_template, line 46
Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    

['/home/anthony/_/dev/django_classr_test',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

Server time:    Wed, 19 Aug 2015 19:27:59 +0000

The error says it can not find a template at static_pages/templates/static_pages/index.html . 该错误表明无法在static_pages/templates/static_pages/index.html找到模板。 But that is exactly where it is? 但这正是它的所在吗? What's going on? 这是怎么回事?

If I do not use render and just return 'hello world' it does not give an error, so I do not think it is a problem with routing. 如果我不使用render而仅返回“ hello world”,则不会给出错误,因此我认为这不是路由问题。

Here is my project settings file: 这是我的项目设置文件:

"""
Django settings for django_classr_test project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

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.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '00000000000000000000000000000000000000000000'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    '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',
    'django.middleware.security.SecurityMiddleware',
)

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


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

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


# Internationalization
# https://docs.djangoproject.com/en/1.8/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.8/howto/static-files/

STATIC_URL = '/static/'

So, I'm guessing I don't have my settings configured correctly? 所以,我猜我的设置没有正确配置吗?

You need to add your app to the installed apps and it will find your templates folder automatically. 您需要将您的应用程序添加到已安装的应用程序中,它将自动找到您的模板文件夹。

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'static_pages',
)

the APPS_DIR setting takes care of loading anything in the templates directory for any app you have installed without specifically specifying the directory. APPS_DIR设置负责为您安装的任何应用程序在templates目录中加载任何内容,而无需特别指定目录。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM