Django扩展全局模板
我是Django新手,在Django扩展全局模板,django,django-templates,django-views,Django,Django Templates,Django Views,我是Django新手,在Django 1.11 我已经创建了三个应用程序页面,用户,搜索,目录结构如下 myapp |- pages |- templates |- pages |- home.html |- urls.py |- views.py |- ... |- search |- templates |- search |- index.html |- myapp |- settings.py
Django 1.11
我已经创建了三个应用程序页面
,用户
,搜索
,目录结构如下
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中
contain
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 %}
{% 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设置(在settings.py文件中)的项目文件夹中添加templates目录:
]您的模板设置是什么样子的?-问题中包含
模板
如何从目录中加载与模板相同级别的静态
文件
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 = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
},
},