A collection of Django templatetags and middleware.
Project description
django-vrot
A collection of Django templatetags and middleware for common web development tasks.
Features
- 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
Installation
uv add django-vrot
Quick Start
- Add
vrotto 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>
Usage
Template Tags
Load the template tags in your templates:
{% load vrot %}
localtime - Display times in user's timezone
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>
humantime - Human-friendly time display
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)
active_link - Highlight active menu items
<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)
query_param_replace - Preserve query parameters
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.
getitem - Access dictionary/list items
Access dictionary values with dynamic keys:
{{ my_dict|getitem:user_provided_key }}
Middleware
TimezoneMiddleware
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.
How Local Timezone Display Works
- The included JavaScript sets a cookie with the user's timezone
- The
TimezoneMiddlewarereads this cookie and activates the timezone in Django - The
localtimefilter 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/
Requirements
- Django >= 3.2
- Python >= 3.9
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_vrot-0.2.0.tar.gz.
File metadata
- Download URL: django_vrot-0.2.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
454475375e62ef9709bf391a60b8fb1ec90671c2885cefe2b666b29174c527dd
|
|
| MD5 |
964c691c8673601a741ddc02634cc6a1
|
|
| BLAKE2b-256 |
07125d4cd73a6d84e34e875420a4c36e097b00e33e96fa7df83acf8e339bcd5b
|
File details
Details for the file django_vrot-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_vrot-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98756799b2227cc2c746fb079a51153a6c883160705ac5dd918fd7ce4e2c5c8c
|
|
| MD5 |
b6cf6493b57a9c16022a274291fba1cb
|
|
| BLAKE2b-256 |
01475838ba714e129f9478d99b5a9ef5e09f3c07096541f9bc25b31eb0f62d12
|