为了账号安全,请及时绑定邮箱和手机立即绑定

TemplateDoesNotExist at /blog/index/

TemplateDoesNotExist at /blog/index/

blog/index.html

Request Method: GET

Request URL: http://127.0.0.1:8000/blog/index/

Django Version: 1.11.5

Exception Type: TemplateDoesNotExist

Exception Value:

blog/index.html

Exception Location: C:\Python27\lib\site-packages\django\template\loader.py in get_template, line 25

Python Executable: C:\Python27\python.exe

Python Version: 2.7.13

Python Path:

['d:\\myblog',

 'C:\\Windows\\system32\\python27.zip',

 'C:\\Python27\\DLLs',

 'C:\\Python27\\lib',

 'C:\\Python27\\lib\\plat-win',

 'C:\\Python27\\lib\\lib-tk',

 'C:\\Python27',

 'C:\\Python27\\lib\\site-packages',

 'C:\\Python27\\lib\\site-packages\\win32',

 'C:\\Python27\\lib\\site-packages\\win32\\lib',

 'C:\\Python27\\lib\\site-packages\\Pythonwin']

Server time: Mon, 8 Jan 2018 15:20:32 +0000

Template-loader postmortem


Django tried loading these templates, in this order:


Using engine :

This engine did not provide a list of tried templates.



'DIRS': [os.path.join(BASE_DIR, 'blog/templates')],这里已经修改过来了,不知道为啥还出错


INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'blog',

]




view.py

from __future__ import unicode_literals


from django.shortcuts import render

from django.http import HttpResponse

import datetime


# def hello(request):

#     now = datetime.datetime.now()

#     html = '<html><body><h3>Hello World</h3>It is now %s</body></html>' % now

#     return HttpResponse(html)


def index(request):

    render(request, 'blog/index.html')



myblog/urls.py

from django.conf.urls import url, include

from django.contrib import admin

import blog.views as bv


urlpatterns = [

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

    url(r'^blog/', include('blog.urls')),

    # 这里不能带$结尾,不然会出错的

]



blog/urls.py

from django.conf.urls import url

from django.contrib import admin

import blog.views as bv


urlpatterns = [

    url(r'^index/$', bv.index),

]



index.html

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<body>

<h1>

Hello , Blog!

</h1>

</body>

</html>



settings.py

"""

Django settings for myblog project.


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


For more information on this file, see

https://docs.djangoproject.com/en/1.11/topics/settings/


For the full list of settings and their values, see

https://docs.djangoproject.com/en/1.11/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.11/howto/deployment/checklist/


# SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = 's9%3bh@p&^9!f6==xxi+l3evc^=8)+-a@@rukwuw2b%--(wvi$'


# 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',

    'blog',

]


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


TEMPLATE = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        'DIRS': [os.path.join(BASE_DIR, 'blog/templates')],

        # '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 = 'myblog.wsgi.application'



# Database

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


DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

    }

}



# Password validation

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


STATIC_URL = '/static/'


正在回答

3 回答

我遇到了同样的问题,请问你解决了吗

0 回复 有任何疑惑可以回复我~

https://img1.sycdn.imooc.com//5aea85cf0001ff3a04530293.jpgsetting 里没有加app名称

2 回复 有任何疑惑可以回复我~

关于模板的定位网上不是有么,只要你的app注册了,运行寻找模板的时候会首先在app的模板中寻找,没有的话会在默认指定的目录寻找,最后是setting同级别的模板中。我当时也遇到这个问题了,找不到模板,我的问题没有修改那个路径,重新启动一次服务就能看到了。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
django入门与实践
  • 参与学习       65127    人
  • 解答问题       898    个

手把手带你进入Django开发的大门,充分领略Django的魅力

进入课程

TemplateDoesNotExist at /blog/index/

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号