Django模板不存在不检查顶级.html?

时间:2015-01-18 05:32:16

标签: python django django-templates

我试图将Django指向mysite的顶级about.html,但是Django似乎没有检查mysite / about.html,mysite / templates / about.html上的文件或mysite / templates / mysite / about.html(我已经在所有这三个地方放了about.html)

我收到TemplateDoesNotExist错误:

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
c:\users\jerry hou\documents\projects\django-trunk\django\contrib\admin\templates\about.html (File does not exist)
c:\users\jerry hou\documents\projects\django-trunk\django\contrib\auth\templates\about.html (File does not exist)
c:\Users\Jerry Hou\Documents\Projects\mysite\polls\templates\about.html (File does not exist)
 #Why doesn't check inside mysite\mysite\ but only in mysite\polls\?

这里是Django mysite的文件目录结构:

mysite/
        manage.py
        polls/
        __init__.py
        admin.py
        migrations/
            __init__.py
        models.py
        tests.py
        views.py
        mysite/
            __init__.py
            settings.py
            urls.py
            wsgi.py

mysite的/ mysite的/ urls.py:

from django.conf.urls import include, url
from django.contrib import admin
from mysite import views

urlpatterns = [
    # Examples:
    url(r'^$', 'mysite.views.index', name='mysite_home'),
    url(r'^about/$', views.AboutView.as_view(), name='mysite_about'),
    url(r'^polls/', include('polls.urls', namespace = "polls")),
    url(r'^admin/', include(admin.site.urls)),

]

2 个答案:

答案 0 :(得分:1)

您应该在项目根目录中创建templates目录,并将以下设置添加到mysite/settings.py

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

TEMPLATE_DIRS文档为here

答案 1 :(得分:1)

以下对我有用:

mysite/
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __pycache__
│   │      ...<omitted>...
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
└── templates
    └── mysite
        └── index.html

mysite的/ mysite的/ settings.py:

"""
Django settings for mysite project.

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

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

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

####CHECK THIS OUT####
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-d^1m(a3a*^$*m@v20_r$66bqy29*q6m#r)!-s)tv7y^#jy%qa'

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

TEMPLATE_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',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'


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

STATIC_URL = '/static/'

mysite的/ mysite的/ views.py:

from django.http import HttpResponse
from django.template import Context, loader

def index(request):
    #return HttpResponse('hello world')

    templ = loader.get_template('mysite/index.html') #Use the path within the templates dir 
    context = Context(
        {'planet': 'world'}
    )
    return HttpResponse(
        templ.render(context)
    )

mysite的/模板/ mysite的/ index.html中:

<div>Hello</div>
<div>{{planet}}</div>

mysite的/ mysite的/ urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

from mysite import views  #NOTE THIS*********

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^$', views.index),

    url(r'^admin/', include(admin.site.urls)),
)
  

为什么不在mysite \ mysite中查看?

您可以通过将settings.py更改为:

来使django执行此操作
...

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'mysite/templates'),
)

...

...并在此处添加模板目录:

mysite
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __pycache__
│   │   ...<omitted>...
│   ├── settings.py
│   ├── templates
│   │   └── index.html

他们的mysite / mysite / views.py看起来像这样:

from django.http import HttpResponse
from django.template import Context, loader

def index(request):
    #return HttpResponse('hello world')

    templ = loader.get_template('index.html') #Use the path within the templates dir 
    context = Context(
        {'planet': 'world'}
    )
    return HttpResponse(
        templ.render(context)
    )