Django Extensions is a comprehensive collection of custom extensions that enhance the Django web framework with additional management commands, utilities, and developer tools.
—
Django Extensions provides template tag libraries that enhance Django templates with debugging capabilities, syntax highlighting, text formatting, and typography enhancements. These template tags are designed to improve the development experience and provide additional formatting options for template output.
Template debugging utilities that integrate popular Python debuggers directly into Django templates.
# Load the library in templates:
# {% load debugger_tags %}
# Available filters:
def ipdb(value):
"""
Interactive Python debugger filter.
Usage in templates:
{{ variable|ipdb }}
Starts an IPython debugger session when the template is rendered.
Requires 'ipdb' package: pip install ipdb
"""
def pdb(value):
"""
Python debugger filter.
Usage in templates:
{{ variable|pdb }}
Starts a standard Python debugger session when the template is rendered.
Uses Python's built-in pdb module.
"""
def wdb(value):
"""
Web debugger filter.
Usage in templates:
{{ variable|wdb }}
Starts a web-based debugger session when the template is rendered.
Requires 'wdb' package: pip install wdb
"""Usage examples:
{% load debugger_tags %}
<!-- Debug a variable with IPython debugger -->
<p>User: {{ user|ipdb }}</p>
<!-- Debug with standard Python debugger -->
<p>Context data: {{ request|pdb }}</p>
<!-- Debug with web debugger -->
<p>Form data: {{ form|wdb }}</p>
<!-- Only debug in development -->
{% if debug %}
{{ complex_data|ipdb }}
{% endif %}Syntax highlighting capabilities for displaying code in templates with proper formatting and colors.
# Load the library in templates:
# {% load highlighting %}
# Available tags and filters:
def highlight(parser, token):
"""
Highlight code blocks with syntax coloring.
Usage in templates:
{% highlight "python" %}
def hello_world():
print("Hello, World!")
{% endhighlight %}
{% highlight "javascript" linenos=True %}
function greet() {
console.log("Hello!");
}
{% endhighlight %}
Parameters:
- language: Programming language for syntax highlighting
- linenos: Show line numbers (optional, default: False)
Requires 'Pygments' package: pip install Pygments
"""
def pygmentize(code, language=None):
"""
Pygments-based syntax highlighting filter.
Usage in templates:
{{ code|pygmentize:"python" }}
{{ javascript_code|pygmentize:"javascript" }}
Parameters:
- code: The code string to highlight
- language: Programming language for syntax highlighting
Returns highlighted HTML with CSS classes for styling.
"""Usage examples:
{% load highlighting %}
<!-- Highlight Python code block -->
{% highlight "python" %}
from django.shortcuts import render
def my_view(request):
context = {'message': 'Hello World'}
return render(request, 'template.html', context)
{% endhighlight %}
<!-- Highlight with line numbers -->
{% highlight "javascript" linenos=True %}
document.addEventListener('DOMContentLoaded', function() {
console.log('Page loaded');
});
{% endhighlight %}
<!-- Highlight code from variable -->
{{ user_code|pygmentize:"python" }}
<!-- Dynamic language selection -->
{% for snippet in code_snippets %}
<h3>{{ snippet.title }}</h3>
{% highlight snippet.language %}{{ snippet.code }}{% endhighlight %}
{% endfor %}
<!-- CSS styling (include in your stylesheet) -->
<style>
.highlight { background: #f8f8f8; }
.highlight .c { color: #408080; font-style: italic } /* Comment */
.highlight .k { color: #008000; font-weight: bold } /* Keyword */
.highlight .s { color: #ba2121 } /* String */
</style>Advanced syntax coloring filters with multiple output formats and styling options.
# Load the library in templates:
# {% load syntax_color %}
# Available filters:
def colorize(code, language):
"""
Colorize code with syntax highlighting using CSS classes.
Usage in templates:
{{ code|colorize:"python" }}
{{ sql_query|colorize:"sql" }}
Parameters:
- code: The code string to colorize
- language: Programming language for syntax detection
Returns HTML with CSS classes for styling.
"""
def colorize_table(code, language):
"""
Colorize code with line numbers in HTML table format.
Usage in templates:
{{ code|colorize_table:"python" }}
Parameters:
- code: The code string to colorize
- language: Programming language for syntax detection
Returns HTML table with line numbers and syntax highlighting.
"""
def colorize_noclasses(code, language):
"""
Colorize code without CSS classes (inline styles).
Usage in templates:
{{ code|colorize_noclasses:"python" }}
Parameters:
- code: The code string to colorize
- language: Programming language for syntax detection
Returns HTML with inline styles instead of CSS classes.
"""Usage examples:
{% load syntax_color %}
<!-- Basic syntax coloring -->
{{ python_code|colorize:"python" }}
<!-- With line numbers in table format -->
{{ large_code_block|colorize_table:"javascript" }}
<!-- Self-contained styling (no external CSS needed) -->
{{ snippet|colorize_noclasses:"css" }}
<!-- Dynamic code display -->
{% for file in source_files %}
<div class="code-block">
<h4>{{ file.name }}</h4>
{{ file.content|colorize:file.language }}
</div>
{% endfor %}Text indentation utilities for formatting content with proper spacing.
# Load the library in templates:
# {% load indent_text %}
# Available tags:
def indentby(parser, token):
"""
Indent text content by specified number of spaces or custom string.
Usage in templates:
{% indentby 4 %}
This text will be indented by 4 spaces.
Multiple lines are all indented.
{% endindentby %}
{% indentby " " %}
Custom indentation string.
{% endindentby %}
{% indentby tab %}
Use tabs for indentation.
{% endindentby %}
Parameters:
- indent_value: Number of spaces, "tab" for tabs, or custom string
"""Usage examples:
{% load indent_text %}
<!-- Indent by 4 spaces -->
<pre>
{% indentby 4 %}
def function():
print("This will be indented")
return True
{% endindentby %}
</pre>
<!-- Indent with tabs -->
<pre>
{% indentby tab %}
if condition:
return value
else:
return default
{% endindentby %}
</pre>
<!-- Custom indentation -->
<pre>
{% indentby ">> " %}
This line starts with ">> "
This line also starts with ">> "
{% endindentby %}
</pre>
<!-- Nested indentation -->
<pre>
Main content:
{% indentby 2 %}
Level 1 indentation
{% indentby 2 %}
Level 2 indentation (4 spaces total)
{% endindentby %}
Back to level 1
{% endindentby %}
</pre>Typography filters that prevent "widow" words (single words on their own line) by adding non-breaking spaces.
# Load the library in templates:
# {% load widont %}
# Available filters:
def widont(text):
"""
Add non-breaking space between final two words to prevent widows.
Usage in templates:
{{ article.title|widont }}
{{ heading_text|widont }}
Parameters:
- text: The text string to process
Returns text with between the last two words.
"""
def widont_html(html_text):
"""
HTML-aware version of widont that preserves HTML tags.
Usage in templates:
{{ article.content|widont_html }}
{{ formatted_text|widont_html }}
Parameters:
- html_text: HTML string to process
Returns HTML with between final two words, preserving tags.
"""Usage examples:
{% load widont %}
<!-- Prevent widow words in headings -->
<h1>{{ article.title|widont }}</h1>
<h2>{{ section.heading|widont }}</h2>
<!-- Process HTML content while preserving tags -->
<div class="content">
{{ article.body|widont_html }}
</div>
<!-- Use in loops -->
{% for post in posts %}
<article>
<h3>{{ post.title|widont }}</h3>
<div class="excerpt">{{ post.excerpt|widont_html }}</div>
</article>
{% endfor %}
<!-- Before widont filter -->
<h1>This is a Very Long Title That Might Break</h1>
<!-- Might render with "Break" alone on the last line -->
<!-- After widont filter -->
<h1>This is a Very Long Title That Might Break</h1>
<!-- "Might Break" will stay together --><!-- Load multiple tag libraries -->
{% load debugger_tags highlighting syntax_color widont %}
<!-- Complete code display example -->
<div class="code-example">
<h3>{{ example.title|widont }}</h3>
{% if debug %}
<!-- Debug the code content -->
{{ example.code|pdb }}
{% endif %}
<!-- Display with syntax highlighting -->
{% highlight example.language linenos=True %}{{ example.code }}{% endhighlight %}
<!-- Or use filter version -->
<div class="code-block">
{{ example.code|colorize_table:example.language }}
</div>
</div>
<!-- Formatted text with proper indentation -->
<pre class="formatted-output">
{% indentby 2 %}
{{ output_text|widont }}
{% endindentby %}
</pre>
<!-- Development debugging template -->
{% if settings.DEBUG %}
<div class="debug-panel">
<h4>Debug Information</h4>
<p>Request: {{ request|ipdb }}</p>
<p>Context: {{ view|pdb }}</p>
<p>Form: {{ form|wdb }}</p>
</div>
{% endif %}/* Basic syntax highlighting styles */
.highlight {
background: #f8f8f8;
border: 1px solid #ddd;
padding: 10px;
border-radius: 4px;
}
.highlight .c { color: #408080; font-style: italic; } /* Comment */
.highlight .k { color: #008000; font-weight: bold; } /* Keyword */
.highlight .s { color: #ba2121; } /* String */
.highlight .n { color: #000; } /* Name */
.highlight .o { color: #666; } /* Operator */
/* Line number table styling */
.highlight table { width: 100%; }
.highlight .linenos {
background: #f0f0f0;
padding-right: 10px;
text-align: right;
user-select: none;
}
/* Code block container */
.code-block {
margin: 1em 0;
overflow-x: auto;
}
.code-block pre {
margin: 0;
white-space: pre-wrap;
}<!-- Conditional debugging based on user permissions -->
{% if user.is_staff %}
{{ complex_query|ipdb }}
{% endif %}
<!-- Multi-language code display -->
{% for language, code in code_samples.items %}
<div class="language-example">
<h4>{{ language|title }}</h4>
{{ code|colorize:language }}
</div>
{% endfor %}
<!-- Formatted documentation -->
<div class="documentation">
{% for section in doc_sections %}
<h3>{{ section.title|widont }}</h3>
<div class="description">{{ section.description|widont_html }}</div>
{% if section.code_example %}
{% highlight section.language %}{{ section.code_example }}{% endhighlight %}
{% endif %}
{% endfor %}
</div>
<!-- Interactive code editor display -->
<div class="code-editor">
<div class="editor-header">{{ file.name|widont }}</div>
<div class="editor-content">
{{ file.content|colorize_table:file.extension }}
</div>
</div>Install with Tessl CLI
npx tessl i tessl/pypi-django-extensions