2016-02-25 92 views
1

我使用linuxpython 3.4django 1.8.2pytmysqlDjango的渲染模板不存在

在我的virtualenv有:

db.sqlite3manage.pynew/templates/


settings.py:

First我评论'DIRS':[],

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', 
      ], 
     }, 
    }, 
] 

然后加入这一部分:

TEMPLATE_DIRS = ( 
     '/home/niloofar/django/niloofar/new/templates',  
) 

urls.py:

从new.views导入欢迎

urlpatterns = [ 
    url(r'^welcome', welcome), 
] 

views.py:

from django.shortcuts import render 

def welcome(request): 

    message = "welcome again:D" 
    return render(request, 'base.html', {'message': message}) 

在模板目录中,有base.html文件文件:

<html> 
<body> 
<p>* {{ message }} *</p> 
</body> 
</html> 

当我刷新页面,它打印错误:

Exception Type: TemplateDoesNotExist

Exception Value: base.html

+0

什么是你的'base.html'的完整路径? –

+0

/home/niloofar/django/niloofar/new/templates/base.html – niloofar

+0

试试$ ls -a/home/niloofar/django/niloofar/new/templates。你得到了什么? –

回答

3

试试这个,不需要评论DIRS。 不推荐使用TEMPLATE_DIRS设置。看链接https://docs.djangoproject.com/en/1.9/ref/settings/#template-dirs

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(os.path.dirname(__file__), '..//', 'templates').replace('\\', '/')], 
     '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', 
      ], 
     }, 
    }, 
] 
+0

如果你认为这个解决方案帮助你,然后批准它与绿色标记 – Kjjassy

+0

是有帮助谢谢。 – niloofar

+0

@niloofar欢迎:) – NiviD