Is there a way to use the {{date|timesince}}
filter, but instead of having two adjacent units, only display one?
For example, my template is currently displaying "18 hours, 16 minutes". How would I get it to display "18 hours"? (Rounding is not a concern here.) Thank you.
In Sendwithus you can use the pluralize filter to change the suffix of a word. Like Django's pluralize filter it returns a plural suffix if the first argument is an integer greater than 1 or a sequence with more than 1 item. If a plural suffix isn't specified pluralize defaults to 's'.
Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.
autoescape. Controls the current auto-escaping behavior. This tag takes either on or off as an argument and that determines whether auto-escaping is in effect inside the block. The block is closed with an endautoescape ending tag.
Use {{ obj | capfirst }} to make uppercase the first character only. Using {{ obj | title }} makes it Camel Case.
I can't think of a simple builtin way to do this. Here's a custom filter I've sometimes found useful:
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def upto(value, delimiter=None):
return value.split(delimiter)[0]
upto.is_safe = True
Then you could just do
{{ date|timesince|upto:',' }}
Since the timesince filter doesn't accept any arguments, you will have to manually strip off the hours from your date.
Here is a custom template filter you can use to strip off the minutes, seconds, and microseconds from your datetime object:
#this should be at the top of your custom template tags file
from django.template import Library, Node, TemplateSyntaxError
register = Library()
#custom template filter - place this in your custom template tags file
@register.filter
def only_hours(value):
"""
Filter - removes the minutes, seconds, and milliseconds from a datetime
Example usage in template:
{{ my_datetime|only_hours|timesince }}
This would show the hours in my_datetime without showing the minutes or seconds.
"""
#replace returns a new object instead of modifying in place
return value.replace(minute=0, second=0, microsecond=0)
If you haven't used a custom template filter or tag before, you will need to create a directory in your django application (i.e. at the same level as models.py and views.py) called templatetags
, and create a file inside it called __init__.py
(this makes a standard python module).
Then, create a python source file inside it, for example my_tags.py
, and paste the sample code above into it. Inside your view, use {% load my_tags %}
to get Django to load your tags, and then you can use the above filter as shown in the documentation above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With