2016-02-06 52 views
2

我是Django的新手。我在Python 2.7中使用Django 1.8.6。我正在嘗試使用一個base.html模板,該模板可以在整個網站中全局使用,每個應用程序都可以訪問它並訪問它。這裏是我的測試網站的當前結構:Django Global base.html模板

twms

投票

遷移

靜態

模板

項目

遷移

靜態

模板

項目

的index.html

tmws

靜態

模板

tmws

base.html文件

這裏是項目/模板/項目/ index.html的

{% extends 'tmws/base.html' %} 
{% block content %} 
    <h1>Projects</h1> 
    <ul> 
    {% for project in project_list %} 
     <li><a href="{% url 'project:detail' project.id %}">{{ project.name }}</a></li> 
    {% endfor %} 

    </ul> 
    end of list 
{% endblock %} 

代碼這是我收到的錯誤:

TemplateDoesNotExist在/項目/

tmws/base。 html

如何從我的任何應用程序訪問tmws/tmws/templates/tmws/base.html?

任何幫助將不勝感激!

讓我知道是否需要任何其他信息。

編輯

這裏是我的模板設置從settings.py:在您的設置

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [ 
      os.path.join(BASE_DIR, 'templates'), # If i leave both or just comment one one out I still get the same error 
      'tmws.tmws.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', 
      ], 
     }, 
    }, 
] 
+0

你有什麼在你的模板設置? –

回答

6

我想你可能會遇到你的模板目錄配置有問題。在您的project's的settings.py嘗試檢查,如果您有類似這樣的配置:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'your template dir name')] 
     , 
     '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', 
      ], 
     }, 
    }, 
] 

'your template dir name'應該tmws。當你嘗試在你的HTML中擴展模板時,這會讓django搜索這個目錄。您可以根據需要添加任意數量的目錄。

我覺得現在的Django必須尋找模板:

twms/project/templates/project 

因此,也許,如果你把你的base.html文件文件中有Django將能夠找到它。

另一個建議是創建一個通用模板目錄,並將base.html放置在那裏,因爲您希望它在整個站點中使用。這只是我對訂購模板的嘗試,但它應該以任何方式工作。

編輯:

添加在settings.py以下解決了這個問題。閱讀評論的更多信息:

MASTER_BASE_DIR = os.path.dirname(__file__) 




TEMPLATES = [ 
     { 
      'BACKEND': 'django.template.backends.django.DjangoTemplates', 
      'DIRS': [os.path.join(MASTER_BASE_DIR, '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', 
       ], 
      }, 
     }, 
    ] 
+0

謝謝你的回覆。請檢查我的編輯。 – user908759

+1

不客氣。你可以嘗試加入:'os.path.join(BASE_DIR,'twms/templates')'或'os.path.join(BASE_DIR,'twms.templates')'。我不確定是否可以在那裏使用點符號。 –

+0

感謝您的幫助,但我仍然遇到同樣的錯誤。出於某種原因,我無法離開項目目錄。我不確定它是否可能與BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__ file__)))有關。我已經嘗試了很多方法來操縱這個,但是當我得到這個頁面不可用時出現錯誤 – user908759

0

你需要的所有模板文件夾添加到模板設置settings.py中。然後Django會將它們視爲它們都在同一個文件夾中。好像你還沒有添加所有這些。你想你的模板看起來是這樣的:

TEMPLATES = [ 
    { 
     'DIRS': [ 
      'twms.tmws.templates', # Since this is not in an app 
     ], 
     'APP_DIRS': True, # Automatically include the templates from INSTALLED_APPS 
    }, 
] 
+0

謝謝你的回覆。請檢查我的編輯。 – user908759