Hello everyone, I am trying to create a template filter that converts a date into human-readable information, such as “date since.”
The code for displaying the published date is as follows:
{{ post.published_date|date_since }}.
I am currently encountering an error that I do not understand.
line 10
DAYS_PER_YEAR = 365
IndentationError: unexpected indent
Thanks.
from datetime import datetime
from django import template
from django.utils import timezone
register = template.Library()
"""FILTERS"""
DAYS_PER_YEAR = 365
DAYS_PER_MONTH = 30
DAYS_PER_WEEK = 7
@register.filter(is_safe=True)
def date_since(specific_date):
#Returns number of days between today and value.
today = timezone.now().date()
if isinstance(specific_date, datetime):
specific_date = specific_date.date()
diff = today - specific_date
diff_years = int(diff.days / DAYS_PER_YEAR)
diff_months = int(diff.days / DAYS_PER_MONTH)
diff_weeks = int(diff.days / DAYS_PER_WEEK)
diff_map = [
("year", "years", "diff_years",),
("month", "months", "diff_months",),
("week", "weeks", "diff_weeks",),
("day", "days", "diff_days",),
]
for parts in diff_map:
(interval, intervals, count,) = parts
if count > 1:
return_("yesterday") \
if interval == "day" \
else _(f"last {interval}")
if diff.days == 0:
return_("today")
else:
# Date is in the future; return formatted date.
return f"{specific_date:%B %d, %Y}"