TemplateDoesNotExist at /有什么问题?

时间:2017-09-22 04:20:37

标签: python django

我收到错误,TemplateDoesNotExist在/ app / index.html或index.html。 应用程序(子应用程序)在testapp(父应用程序)中,应用程序的index.html·urls.py·views.py .urls.py的应用程序是

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


    urlpatterns = [
        url(r'^$', views.index, name='index'),
    ]
views.py of app is 
from django.shortcuts import render


def index(request):
    return render(request, 'app/index.html')

testapp的urls.py是

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

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', index, name='index'),
]

渲染的app中的views.py的参数被改为'index.html',但是同样的错误发生了。出了什么问题?(写目录的方法是什么?) 我该怎么解决这个问题? Traceback说

Traceback:

File "/Users/XXX/myenv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/Users/XXX/myenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/XXX/myenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/XXX/testapp/app/views.py" in index
  5.     return render(request, 'index.html')

File "/Users/XXX/myenv/lib/python3.5/site-packages/django/shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "/Users/XXX/myenv/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
  67.         template = get_template(template_name, using=using)

File "/Users/XXX/myenv/lib/python3.5/site-packages/django/template/loader.py" in get_template
  25.     raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /
Exception Value: index.html

2 个答案:

答案 0 :(得分:0)

HI你的模板应该在(yourapp / templates / index.html)

检查 settings.py 是否具有以下配置

INSTALLED_APPS = ['your_app_name']

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',
                'django.template.context_processors.media'
            ],
        },
    },
]

然后是 view.py

def index(request):
    return render(request, 'index.html')

答案 1 :(得分:0)

在设置文件

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

意味着Django将查看项目下templates /目录中的模板。

<强> OR

默认情况下可能未定义SETTINGS_PATH。在这种情况下,您需要定义它(在settings.py中):

import os

SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))

如果你想这样的话

<强> 应用程序/ index.html的

然后你必须在模板目录中创建app文件夹

试试这个

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['/your/dir/here/templates',],
    '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',
        ],
    },
},