我Django 1.11
Django的延長全局模板
我已經創建了三個應用pages
,users
是新來的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',
],
},
},
]
什麼是您的模板設置是什麼樣子? - –
包含有問題的「TEMPLATES」 –