繁体   English   中英

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

[英]Django Template Doesn't Exist doesn't check top-level .html?

我试图将Django指向mysite的顶级about.html,但是Django似乎并未在mysite / about.html,mysite / templates / about.html或mysite / templates / mysite /中检查文件about.html(我在所有这3个地方都放了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)),

]

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

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

TEMPLATE_DIRS文档在这里

以下为我工作:

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 / templates / 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)
    )

暂无
暂无

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

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