我正在将django 2.1.3运行到基于高山图像的python3容器中,我按照以下指南创建了自己的图像:https://docs.docker.com/compose/django/(与其他项目一起工作)这次不显示模板,我收到的页面是被调用模板的渲染,扩展部分不存在。
我尝试在绑定文件夹而不是docker卷中运行代码,并且没有变化。
Dockerfile:
FROM python:3-alpine
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /code/
WORKDIR /code/
ADD requirements.txt /
RUN apk update && \
apk add postgresql-libs && \
apk add --virtual .build-deps gcc musl-dev postgresql-dev && \
python3 -m pip install -r /requirements.txt --no-cache-dir && \
apk --purge del .build-deps
docker-compose(Django图像部分):
django:
build: "./django"
image: registry.gitlab.com/vaschetto.marco/project-docker-File/django:v0.1
container_name: Project_django
restart: always
command: python3 manage.py runserver 0.0.0.0:8000
env_file:
- ./django/.env
volumes:
- django:/code/
- django_static:/code/static/
depends_on:
- psql
模板结构:
.
├── db.sqlite3
├── Dockerfile
├── project
...
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── requirements.txt
├── sito #app
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
...
│ ├── models.py
│ ├── __pycache__
...
│ ├── templates
│ │ └── sito
│ │ ├── body.html
│ │ ├── footer.html
│ │ ├── head.html
│ │ └── home.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
└── static
django项目设置(已安装的应用程序和模板部分):
...
# Application definition
INSTALLED_APPS = [
'sito',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
...
project / urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('sito.urls')),
path('admin/', admin.site.urls),
]
sito / urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
sito / views.py
from django.shortcuts import render
# Create your views here
def index(request):
return render(request, "sito/home.html")
sito / templates / sito / home.html:
<!DOCTYPE html>
<html>
<head>
{% block head %}
{% endblock %}
<head>
<body>
{% block body %}
{% endblock %}
</body>
<footer>
{% block footer %}
{% endblock %}
</footer>
</html>
sito / templates / sito / head.html:
{% extends "sito/home.html" %}
{% block head %}
<title>
This is the title WEBISTE!
<title>
{% endblock %}
sito / templates / sito / body.html:
{% extends "sito/home.html" %}
{% block body %}
<h1>WEB SITE BODY!</h1>
{% endblock %}
sito / templates / sito / footer.html:
{% extends "sito/home.html" %}
{% block footer %}
<h2>THIS IS THE FOOTER</h2>
{% endblock %}
我期望的结果是这样的:
<!DOCTYPE html>
<html>
<head>
<title>
This is the title WEBISTE!
<title>
<head>
<body>
<h1>WEB SITE BODY!</h1>
</body>
<footer>
<h2>THIS IS THE FOOTER</h2>
</footer>
</html>
但渲染器返回此页面:
<!DOCTYPE html>
<html>
<head>
<head>
<body>
</body>
<footer>
</footer>
</html>
答案 0 :(得分:1)
您的html模板部分在概念上是错误的。您要求呈现home.html
的内容占位符。您必须通过扩展模板来填充这些占位符,并在呈现之前覆盖它们,因为您没有覆盖它们,所以它们保持为空,这就是为什么您看到空的html块的原因。您从未在模板内使用过它们定义了页眉和页脚。尝试以下方法,可以在正确理解之后扩展您想要的方法。非常简单。
home.html =>从base.html扩展并覆盖block body
====== sito/header.html =======
<title>
This is the title WEBISTE!
</title>
====== sito/footer.html =======
<h2>THIS IS THE FOOTER</h2>
====== sito/base.html =======
<!DOCTYPE html>
<html>
<head>
{% include "sito/header.html" %}
<head>
<body>
{% block body %}
{% endblock %}
<footer>
{% include "sito/footer.html" %}
</footer>
</body>
</html>
现在您可以通过扩展base.html
====== sito/home.html =======
{% extends "sito/base.html" %}
{% block body %}
<h1>WEB SITE BODY!</h1>
{% endblock %}
====== sito/about.html =======
{% extends "sito/base.html" %}
{% block body %}
<h1>This is About us</h1>
{% endblock %}