django-templates
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDjango Template Patterns
Django 模板模式
Template Organization
模板组织结构
templates/
├── base.html # Root template with common structure
├── partials/ # Reusable fragments (navbar, footer, pagination)
├── components/ # UI components (button, card, modal)
└── <app>/ # App-specific templates
├── list.html # Full pages extend base.html
├── detail.html
├── _list.html # HTMX partials (underscore prefix)
└── _form.htmlNaming conventions:
- Full pages: ,
list.html,detail.htmlform.html - HTMX partials: ,
_list.html(underscore prefix)_card.html - Shared partials: ,
partials/_navbar.htmlpartials/_pagination.html - Components: ,
components/_button.htmlcomponents/_modal.html
templates/
├── base.html # 站点通用结构的根模板
├── partials/ # 可复用片段(导航栏、页脚、分页)
├── components/ # UI组件(按钮、卡片、模态框)
└── <app>/ # 应用专属模板
├── list.html # 继承base.html的完整页面
├── detail.html
├── _list.html # HTMX局部模板(以下划线为前缀)
└── _form.html命名规范:
- 完整页面:、
list.html、detail.htmlform.html - HTMX局部模板:、
_list.html(以下划线为前缀)_card.html - 共享局部模板:、
partials/_navbar.htmlpartials/_pagination.html - 组件:、
components/_button.htmlcomponents/_modal.html
Template Inheritance
模板继承
Use three-level inheritance for consistent layouts:
- base.html - Site-wide structure (HTML skeleton, navbar, footer)
- Section templates - Optional intermediate templates for sections with shared elements
- Page templates - Individual pages that extend base or section templates
Standard blocks to define in base.html:
- - Page title (use
titleto append to site name){{ block.super }} - - Main page content
content - - Page-specific stylesheets
extra_css - - Page-specific scripts
extra_js
使用三级继承来保持布局一致性:
- base.html - 站点全局结构(HTML骨架、导航栏、页脚)
- 板块模板 - 可选的中间层模板,用于包含共享元素的板块
- 页面模板 - 继承基础或板块模板的独立页面
base.html中应定义的标准块:
- - 页面标题(使用
title来追加站点名称){{ block.super }} - - 页面主要内容
content - - 页面专属样式表
extra_css - - 页面专属脚本
extra_js
Partials and Components
局部模板与组件
Partials are template fragments included in other templates. Use for:
- Repeated content (pagination, empty states)
- HTMX responses that replace portions of the page
- Keeping templates DRY
Components are self-contained UI elements with configurable behavior. Pass context with:
{% include "components/_button.html" with text="Submit" variant="primary" %}- Add to isolate context:
only{% include "_card.html" with title=post.title only %}
局部模板是嵌入到其他模板中的模板片段,适用于:
- 重复出现的内容(分页、空状态)
- 用于替换页面部分内容的HTMX响应
- 保持模板代码简洁(DRY原则)
组件是具备可配置行为的独立UI元素,通过以下方式传递上下文:
{% include "components/_button.html" with text="Submit" variant="primary" %}- 添加来隔离上下文:
only{% include "_card.html" with title=post.title only %}
Custom Tags and Filters
自定义标签与过滤器
When to create custom template tags:
- - Return a string value (e.g., active link class, formatted output)
simple_tag - - Render a template fragment (e.g., pagination component, user avatar)
inclusion_tag
When to create custom filters:
- Transform a single value (e.g., initials from name, percentage calculation)
- Chain with other filters for composable transformations
Location: (create in templatetags directory)
apps/<app>/templatetags/<app>_tags.py__init__.py何时创建自定义模板标签:
- - 返回字符串值(例如,激活链接的类名、格式化输出)
simple_tag - - 渲染模板片段(例如,分页组件、用户头像)
inclusion_tag
何时创建自定义过滤器:
- 转换单个值(例如,从姓名提取首字母、百分比计算)
- 可与其他过滤器链式调用,实现组合转换
存放位置: (需在templatetags目录下创建)
apps/<app>/templatetags/<app>_tags.py__init__.pyAnti-Patterns
反模式
Move logic out of templates:
- Complex conditionals belong in views or model methods
- Use not
user.can_moderateuser.role == "admin" or user.role == "moderator"
Use URL names, not hardcoded paths:
- Use not
{% url 'posts:detail' pk=post.pk %}/posts/{{ post.id }}/
Use CSS classes, not inline styles:
- Use not
class="error-message"style="color: red;"
Handle empty states:
- Always include clause in loops that might have no items
{% empty %}
将逻辑移出模板:
- 复杂条件判断应放在视图或模型方法中
- 使用而非
user.can_moderateuser.role == "admin" or user.role == "moderator"
使用URL名称,而非硬编码路径:
- 使用而非
{% url 'posts:detail' pk=post.pk %}/posts/{{ post.id }}/
使用CSS类,而非内联样式:
- 使用而非
class="error-message"style="color: red;"
处理空状态:
- 对于可能无数据的循环,务必包含子句
{% empty %}
Integration with Other Skills
与其他技能的集成
- htmx-patterns - HTMX partial templates and dynamic UI
- django-forms - Form rendering and validation patterns
- pytest-django-patterns - Testing template rendering
- htmx-patterns - HTMX局部模板与动态UI
- django-forms - 表单渲染与验证模式
- pytest-django-patterns - 模板渲染测试