2017-09-10 61 views
0

Django 1.11Django的延長全局模板

我已經創建了三個應用pagesusers是新來的Django和創建第一個應用程序,search和目錄結構

myapp 
|- pages 
    |- templates 
     |- pages 
     |- home.html 
    |- urls.py 
    |- views.py 
    |- ... 
|- search 
    |- templates 
     |- search 
     |- index.html 
|- myapp 
    |- settings.py 
    |- urls.py 
|- static 
    |- css 
     |- style.css 
    |- js 
     |- script.js 
|- templates 
    |- base.html 
|- manage.py 

myapp/pages/views.py包含

from django.views.generic import TemplateView 


class HomeView(TemplateView): 
    template_name = 'pages/home.html' 

myapp/pages/templates/pages/home.html包含

{% extends 'base.html' %} 
{% block content %} 
    <h1>Home page</h1> 
{% endblock %} 

myapp/templates/base.html包含

{% load static %} 
<html> 
    <head> 
    <link href="{% static 'css/style.css' %}"> 
    ... 
    </head> 
    <body> 
    <header> 
     Welcome to my app 
    </header> 
    <div class="container"> 
     {% block content %} 
     {% endblock %} 
    </div> 
    </body> 
</html> 

但是,當我嘗試訪問

http://127.0.0.1:8000/pages 

它給誤差

Exception Type:  TemplateDoesNotExist 
Exception Value: base.html 


Template-loader postmortem 

Django tried loading these templates, in this order: 

Using engine django: 

    django.template.loaders.app_directories.Loader: /path_to_app/seotool/pages/templates/base.html (Source does not exist) 
    django.template.loaders.app_directories.Loader: /usr/local/lib/python3.6/site-packages/django/contrib/admin/templates/base.html (Source does not exist) 
    django.template.loaders.app_directories.Loader: /usr/local/lib/python3.6/site-packages/django/contrib/auth/templates/base.html (Source does not exist) 

如何查看加載base.html

由於所有三個應用程序都將使用常見的導航和靜態文件,因此我不想將其單獨包含在應用程序中。

編輯2>的myapp/MYAPP/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', 
      ], 
     }, 
    }, 
] 
+0

什麼是您的模板設置是什麼樣子? - –

+0

包含有問題的「TEMPLATES」 –

回答

1

你必須從你的項目文件夾中添加模板目錄中的模板設置(在你的settings.py文件):

TEMPLATES = [ 
{ 
    'BACKEND': 'django.template.backends.django.DjangoTemplates', 
    'DIRS': [os.path.join(BASE_DIR, 'templates')], 
    'APP_DIRS': True, 
    'OPTIONS': { 
     # ... some options here ... 
    }, 
}, 

]

+0

如何從'templates'的目錄加載靜態文件' –

+0

@AnujTBE請參閱[如何操作](https://docs.djangoproject.com/en/ 1.10/howto/static-files /)在靜態文件上 – doru