Андрей Asked:2022-07-14 00:53:04 +0000 UTC2022-07-14 00:53:04 +0000 UTC 2022-07-14 00:53:04 +0000 UTC Django模板复制 772 创建了一个包含多个应用程序 ( auth, blog) 的项目。在每个应用程序中,我创建了一个文件夹templates。但base.html对于这些应用程序也是如此。如果有其他选择,不想html多次复制代码? python 1 个回答 Voted Best Answer Leetovskiy 2022-07-14T04:35:55Z2022-07-14T04:35:55Z 模板可以从相邻的应用程序以及当前应用程序继承。以下条目对应用内blog模板和应用内模板(auth如果它base.html位于文件夹中)有效blog: {% extends "blog/base.html" %} 但是我把模板放在项目根目录下的一个公共目录下,在我看来更明显也更方便。为此,您需要在以下目录中添加路径settings.py: TEMPLATES = [ { ... "DIRS": [os.path.join(BASE_DIR, "templates")], ... }, ] 目录结构示例templates: templates ├── blog │ ├── post_creation.html │ ├── post_detail.html │ ├── post_list.html │ ├── post_multiple_creation.html │ └── post_multiple_creation_success.html ├── auth │ ├── logged_out.html │ ├── login.html │ ├── profile_edit.html │ ├── profile.html │ ├── registration.html │ └── registration_success.html ├── 403.html └── base.html
模板可以从相邻的应用程序以及当前应用程序继承。以下条目对应用内
blog模板和应用内模板(auth如果它base.html位于文件夹中)有效blog:但是我把模板放在项目根目录下的一个公共目录下,在我看来更明显也更方便。为此,您需要在以下目录中添加路径
settings.py:目录结构示例
templates: