TemplateDoesNotExist 异常

Posted

技术标签:

【中文标题】TemplateDoesNotExist 异常【英文标题】:TemplateDoesNotExist exception 【发布时间】:2019-08-18 12:32:34 【问题描述】:

我对 Django 很陌生。我在 / 处遇到异常 TemplateDoesNotExist。 我猜我的模板 DIR 有问题。

这是网址:

from django.conf.urls import url
from first_app import views

urlpatterns= [
    url(r'', views.index, name='index'),
]

这是视图:

from django.shortcuts import render
from django.http import HttpResponse


def index(request):
    my_dict='insert_me':"Hello I have created my first dynamic Template"
    return render(request,'Twenty3rdMarch/first_app/templates/index.html',context=my_dict)

这是我的设置:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES_DIR=os.path.join(BASE_DIR,'templates')
print(TEMPLATES_DIR)
ALLOWED_HOSTS = []

# Application definition

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

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 = 'Twenty3rdMrach.urls'

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR,],
        '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 = 'Twenty3rdMrach.wsgi.application'


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

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'udemy',
        'USER':'root',
        'PASSWORD':'Ravi@go123',
        'HOST':'localhost',
        'PORT':'3306',
    



# Password validation
# https://docs.djangoproject.com/en/2.1/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.1/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/2.1/howto/static-files/

STATIC_URL = '/static/'

请帮我解决它。

【问题讨论】:

【参考方案1】:

Twenty3rdMarch/first_app/templates/index.html 更改为index.html。从APP_DIRS 引用模板时不需要完整路径。

不过,我建议您正确命名模板目录。简单地将index.html 放在应用程序的模板目录中可能会导致模板覆盖。您应该在应用程序的模板目录中创建具有相同名称的目录。

myapp
  templates
    myapp
      index.html

然后像myapp/index.html一样引用它

更多信息,请阅读this doc。

【讨论】:

【参考方案2】:

您可以在主项目的文件夹下创建模板文件夹,并将此更改更改为您的 settings.py

TEMPLATES = [

    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')]
    ,
    -----rest of settings ------
]

【讨论】:

以上是关于TemplateDoesNotExist 异常的主要内容,如果未能解决你的问题,请参考以下文章

在 / 处获取 TemplateDoesNotExist

python Django问题:TemplateDoesNotExist at /index/

Django 管理站点:TemplateDoesNotExist?

TemplateDoesNotExist - Django 错误

TemplateDoesNotExist:模板路径错误

TemplateDoesNotExist :在 Django 上未检测到模板