You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django模板复用优化:遵循DRY原则的最佳方案探讨

Great question! Splitting opening/closing div tags into separate includes feels messy for good reason—let’s walk through far cleaner, Django-native solutions to follow the DRY principle here.

1. Template Inheritance (The Go-To for Page-Wide Repeats)

If this repeated div wraps the main content across multiple templates, leverage Django’s core template inheritance system (which is built for exactly this kind of reuse).

In your account/base.html, define a block inside the shared div:

<!-- account/base.html -->
<!DOCTYPE html>
<html>
<head>
    <!-- Your base head content -->
</head>
<body>
    <!-- Other shared elements like navbars -->
    <div class="your-repeated-div-styles">
        {% block main_content %}{% endblock %}
    </div>
    <!-- Other shared footer content -->
</body>
</html>

Then in child templates (e.g., account/profile.html), inherit the base and fill the block:

{% extends "account/base.html" %}

{% block main_content %}
    <!-- Your unique profile content here—no need to retype the div! -->
    <h1>My Profile</h1>
    <p>User-specific content...</p>
{% endblock %}

This is the cleanest approach if the div is a consistent wrapper for entire page sections.

2. Reusable Include with Embedded Blocks (For Localized Repeats)

If the div is repeated in parts of templates (not as a full-page wrapper), create a single include file that holds the div structure and uses a block for variable content.

First, make a component template like components/repeated_div.html:

<!-- components/repeated_div.html -->
<div class="shared-div-class" id="optional-shared-id">
    {% block div_content %}{% endblock %}
</div>

Then in any template that needs this div, use {% include %} with a nested block to inject your unique content:

{% include "components/repeated_div.html" %}
    {% block div_content %}
        <!-- Unique content for this instance -->
        <p>This is inside the shared div!</p>
    {% endblock %}
{% endinclude %}

This keeps the div’s structure in one place, and you only write the content that changes.

3. Custom Template Tags (For Dynamic Divs)

If your div needs dynamic attributes (like changing classes based on context variables), build a custom inclusion tag.

First, create a templatetags directory in your app (don’t forget the __init__.py file), then add custom_tags.py:

from django import template

register = template.Library()

@register.inclusion_tag('components/dynamic_div.html', takes_context=True)
def dynamic_div(context, custom_class="default-div"):
    # Pass context variables if you need them in the div template
    return {
        'custom_class': custom_class,
        'user': context.get('user')
    }

Then make the corresponding component template components/dynamic_div.html:

<!-- components/dynamic_div.html -->
<div class="{{ custom_class }} {% if user.is_authenticated %}authenticated-user{% endif %}">
    {% block div_content %}{% endblock %}
</div>

Now load the tag in your template and use it:

{% load custom_tags %}

{% dynamic_div custom_class="special-div" %}
    <!-- Your dynamic content here -->
    {% if user.is_authenticated %}
        <p>Welcome back!</p>
    {% endif %}
{% enddynamic_div %}

This is perfect when the div’s attributes or structure need to adapt to context.

Final Recommendation

  • Use template inheritance if the div is a consistent wrapper for full page sections.
  • Use include with blocks for localized, static div repeats.
  • Use custom template tags only when you need dynamic behavior.

All of these approaches avoid the awkward split of opening/closing tags into separate files, keeping your template code clean and maintainable.

内容的提问来源于stack exchange,提问作者user9252255

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:56:55