Django找不到模板目录 [英] Django can't find template directory

查看:413
本文介绍了Django找不到模板目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多与此问题类似的问题,但是还没有一个问题解决了我的问题。



Python版本:3.4
Django版本:1.8



加载 :

 模板= [
{
'BACKEND':'django.template.backends.django。 DjangoTemplates',
'DIRS':[TEMPLATE_PATH],
'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。 message',
],
},
},
]


I know there are many questions similar to this one, but none has solved my problem yet.

Python version: 3.4 Django version: 1.8

I get TemplateDoesNotExist at /lfstd/ when loading http://127.0.0.1:8000/lfstd/.

system.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
    TEMPLATE_PATH,
)

lfstd/views.py:

def index(request):
[...]
return render(request, 'lfstd/index.html', context_dict)

project urls.py:

from django.conf.urls import include, url
from django.contrib import admin

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

lfstd urls.py:

from django.conf.urls import patterns, url
from lfstd import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'))

Running print on base_dir and template_path, I get:

Base dir: C:\Users\Phil\PycharmProjects\TownBuddies
Template path: C:\Users\Phil\PycharmProjects\TownBuddies\templates

Which is exactly where my project and template folder is located. However, django doesn't look in that folder for templates. It looks in the following folders:

C:\Python34\lib\site-packages\django\contrib\admin\templates\lfstd\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\lfstd\index.html (File does not exist)

In fact, if I move my template there, it does find it and it works.

Any help is very appreciated, I'm starting out with Django and completely stuck...

EDIT:

I changed TEMPLATE_DIRS to TEMPLATES but Django still isn't looking in the templates folder:

解决方案

TEMPLATE_DIRS setting is deprecated in django 1.8. You should use the TEMPLATES instead:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_PATH],
        '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找不到模板目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆