2016-02-25 94 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