A collection of Django templatetags and middleware for common web development tasks.
- Local timezone display: Automatically display dates and times in the visitor's local timezone
- Active link detection: Highlight active menu items based on current URL
- Query parameter management: Easily modify query parameters while preserving others
- Template utilities: Access dictionary items dynamically and format time displays
uv add django-vrot
- Add
vrot
to yourINSTALLED_APPS
:
INSTALLED_APPS = [
# ...
"vrot",
]
- For timezone support, add the middleware to your
MIDDLEWARE
:
MIDDLEWARE = [
# ...
"vrot.middleware.TimezoneMiddleware",
]
- Include the JavaScript file in your base template (only needed for timezone features):
{% load static %}
<script src="{% static 'vrot/timezone.js' %}" defer></script>
Load the template tags in your templates:
{% load vrot %}
Renders a time element that will be converted to the user's local timezone via JavaScript:
{{ comment.created_at|localtime }}
Output:
<time datetime="2024-05-19T10:34:00+02:00" class="local-time">May 19, 2024 at 10:34 AM</time>
Shows relative time for recent dates:
{{ comment.created_at|humantime }}
Outputs:
- "2 hours ago" (for times less than 24 hours ago)
- "Yesterday at 3:45 PM" (for times 24-48 hours ago)
- Full date display (for older times)
<li class="{% active_link 'blog:index' %}">
<a href="{% url 'blog:index' %}">Blog</a>
</li>
Parameters:
viewname
: The name of the view (including namespace, if any)css_class
: CSS class to apply when active (default: "menu-active")css_inactive_class
: CSS class when inactive (default: "")strict
: If True, requires exact path match (default: False)
Useful for pagination while maintaining filters:
<a href="{% query_param_replace page=page_obj.next_page_number %}">Next Page</a>
This preserves existing query parameters (like filters) while updating the page number.
Access dictionary values with dynamic keys:
{{ my_dict|getitem:user_provided_key }}
Automatically activates the user's timezone based on a cookie set by the JavaScript code. This allows Django to render times in the user's local timezone server-side.
The middleware reads the timezone
cookie and activates the corresponding timezone for the duration of the request.
- The included JavaScript sets a cookie with the user's timezone
- The
TimezoneMiddleware
reads this cookie and activates the timezone in Django - The
localtime
filter renders times with proper timezone information - The JavaScript converts any remaining times to the user's local format
For more details, see: https://www.loopwerk.io/articles/2025/django-local-times/
- Django >= 3.2
- Python >= 3.9
MIT License - see LICENSE file for details.