TemplateDoesNotExist在/

Cra*_*uck 10 django-templates

这是我的网站网址http://webtrick.heliohost.org/ 我的模板目录设置:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__) , 'templates').replace('\\','/')
)
Run Code Online (Sandbox Code Playgroud)

view.py

from django.http import HttpResponse
from django.template import Template , Context
from django.shortcuts import render_to_response
def index(request):
        return render_to_response('index.html')
Run Code Online (Sandbox Code Playgroud)

url.py

from django.conf.urls.defaults import patterns, include, url
from webtrickster.views import index
urlpatterns = patterns('',
    url(r'^$', index)
)
Run Code Online (Sandbox Code Playgroud)

我不知道这个代码有什么问题,任何帮助表示赞赏

pko*_*out 38

确保您的Django应用程序位于settings.py中的INSTALLED_APPS中.这是我的问题.因此,如果您的民意调查应用程序文件夹中有模板文件夹,则需要在设置文件中的已安装应用程序末尾添加"民意调查".


小智 8

将您的应用程序添加到INSTALLED_APPS的setting.py结尾,如下所示:

INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles',
    'mysite'                   
  ]
Run Code Online (Sandbox Code Playgroud)

你的模板目录应该像mysite一样

mysite
    -settings.py
    -templates/
Run Code Online (Sandbox Code Playgroud)


小智 5

 os.path.join(os.path.dirname(__file__) ,'../templates').replace('\\','/')
Run Code Online (Sandbox Code Playgroud)

这对我有用.

  • 它只是我,还是应该在"../ templates"之前有一个引号? (5认同)

小智 5

首先,您需要在django项目文件夹中创建一个名为“ templates”的文件夹,然后,您可以通过两种方式添加模板目录:打开设置文件,然后添加以下任何代码

1)您可以直接通过以下方式指定模板路径

TEMPLATE_DIRS = ( 'D:/Django/web/Kindset/templates' #your file path ,this is my template directory path )

2)或者如果您想使用通用路径意味着使用

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


him*_*uxd 5

我浪费了很多时间试图找出我的'templates'目录出了什么问题并想出了:

您可能忘记添加TEMPLATE_DIR以下部分settings.py

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',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

所以它应该有'DIRS': [TEMPLATE_DIR,],而不是'DIRS': [],

另请参阅以下答案:TemplateDoesNotExist at /