Opened 20 months ago
Last modified 6 days ago
#36307 assigned New feature
Support passing depth to naturaltime, timesince and timeuntil filters
| Reported by: | Giannis Terzopoulos | Owned by: | Ben Cardy |
|---|---|---|---|
| Component: | Template system | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I propose extending these filters to accept an optional depth argument that gets passed downstream:
timesincewould pass depth todjango.utils.timesince.timesince()timeuntilwould pass depth todjango.utils.timesince.timeuntil()naturaltimewould pass depth toNaturalTimeFormatter.string_for(), which would then pass it to the underlyingtimesince/timeuntilcalls
Example:
For a datetime object dt representing 38 days in the past:
{{ dt|naturaltime:"1" }} would return "1 month ago", instead of the current depth=2 output: "1 month, 1 week ago"
Change History (6)
comment:1 by , 20 months ago
| Component: | Uncategorized → Template system |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Version: | → dev |
comment:2 by , 17 months ago
This is less simple than it appears at first.
The timesince and timeuntil filters can already take an optional argument, a variable containing the date to use as the comparison point. Since filters can only take one optional argument there is no room for depth. I've thought about overloading it, but I've concluded it's a bad thing to do.
If people need this functionality in their projects, I think it is okay that they can create their own simple_tag for the job.
The naturaltime filter does not have any arguments, you could add it there, but the justification Sarah used about timesince and timeuntil functions being updated is a little more of a stretch if it is only this filter from humanize that is being updated.
I'd suggest this should be changed to wontfix.
comment:3 by , 8 days ago
Having read the above, I still see value in adding support to the naturaltime filter. It may not be currently possible to expose this to the timesince and timeuntil filters, but I think it's a good flexibility to add to naturaltime as we can.
I've assigned the ticket to myself (as it's been a while since it was last worked on), and written a small PoC PR for this: https://github.com/django/django/pull/22258
Equally, if the consensus is to close the ticket as not worth adding, that's fine.
comment:4 by , 8 days ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
comment:5 by , 6 days ago
| Needs documentation: | set |
|---|
comment:6 by , 6 days ago
| Needs documentation: | unset |
|---|
Since the depth parameter was added to timesince and timeuntil within #31623, I believe this is a logical follow up