Opened 8 years ago

Last modified 9 days ago

#27106 assigned Cleanup/optimization

Document which template filters can be used in Python code (and how)

Reported by: Baptiste Mispelon Owned by: Ryan Cheley
Component: Documentation Version: 1.10
Severity: Normal Keywords:
Cc: Baptiste Mispelon Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The Django template language ships with a lot of filters. Usually they're implemented as Python functions and can be imported and used in your own code.

However this particular usage is not documented (take the example of the various truncate* filters [1]).

I think it would be useful to know:

1) Which template filters can be used as functions;
2) Where to import them from and how to use them;
3) If there are limitations/gotchas related to using them.

[1] https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#truncatechars

Change History (11)

comment:1 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

While I think I've imported from django.template.defaultfilters in some of my own projects, I'm not sure that promoting that pattern as a public API is a good idea. Some of the filters use functions from django.utils.*, e.g. the slugify filter uses django.utils.text.slugify. I don't mind linking to those utility functions from the template tag documentation.

comment:2 by Aymeric Augustin, 8 years ago

Yes, generally speaking Django template filters should be thin wrappers about utility functions. This makes it easy to use the underlying plain function in Jinja2 templates.

Version 0, edited 8 years ago by Aymeric Augustin (next)

comment:3 by Burhan Khalid, 7 years ago

Owner: changed from nobody to Burhan Khalid
Status: newassigned

I'll work a PR for this, where I'll link to the relevant django.utils.* documentation for those filters that are pure wrappers for other functions.

comment:4 by Burhan Khalid, 7 years ago

Has patch: set

https://github.com/django/django/pull/7519

There was some documentation missing for some of the utils functions, which I added to the relevant sections in order to ensure Sphinx internal references were linked correctly.

comment:5 by Mariusz Felisiak, 7 years ago

Patch needs improvement: set

comment:6 by Burhan Khalid, 7 years ago

Patch needs improvement: unset

comment:7 by Tim Graham, 7 years ago

Patch needs improvement: set

comment:8 by Mariusz Felisiak, 3 years ago

Owner: Burhan Khalid removed
Status: assignednew

comment:9 by Ryan Cheley, 5 weeks ago

Owner: set to Ryan Cheley
Status: newassigned

Before I start working on this ticket I wanted to make sure that it was still needed / valid. I can pick up the work that was done previously and try to get it over the finish line.

comment:10 by Natalia Bidart, 11 days ago

Cc: Baptiste Mispelon added

Baptiste, would you have some time to answer the latest question in comment:9?

in reply to:  9 comment:11 by Baptiste Mispelon, 9 days ago

Replying to Ryan Cheley:

Before I start working on this ticket I wanted to make sure that it was still needed / valid. I can pick up the work that was done previously and try to get it over the finish line.

The truncatechars filter linked in the original ticket still doesn't document which utility function it uses under the hood. Based on that, I would say that the ticket is still valid.

Reading comment:1 and comment:2, it seems the direction of the ticket is to:

1) identify the utility functions that are used by the default template filters
2) make sure those utilities are documented (on their respective pages)
3) make sure the template filter documentation links to the corresponding utils documentation

Note: See TracTickets for help on using tickets.
Back to Top