Opened 9 years ago
Last modified 4 months 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 (15)
comment:1 by , 9 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 9 years ago
Yes, generally speaking Django template filters should be thin wrappers around utility functions. This makes it easy to use the underlying plain function in Jinja2 templates.
comment:3 by , 9 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
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 , 9 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 , 9 years ago
| Patch needs improvement: | set |
|---|
comment:6 by , 9 years ago
| Patch needs improvement: | unset |
|---|
comment:7 by , 9 years ago
| Patch needs improvement: | set |
|---|
comment:8 by , 5 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
follow-up: 11 comment:9 by , 20 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
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 , 19 months ago
| Cc: | added |
|---|
Baptiste, would you have some time to answer the latest question in comment:9?
follow-up: 12 comment:11 by , 19 months 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
comment:12 by , 18 months ago
Replying to Baptiste Mispelon:
The
truncatecharsfilter 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
For truncatechars would something like this be what is needed (it would need to be cleaned up to conform to the standards in the docs, but I just want to make sure I’m on the right track)
truncatechars when given a non-HTML string uses unicodedata.normalize documented here https://docs.python.org/3/library/unicodedata.html#unicodedata.normalize
truncatechars when given an HTML string uses HTMLParse from html.parser which is documented here https://docs.python.org/3/library/html.parser.html
comment:13 by , 13 months ago
I'm documenting the work being done here so as to not generate too much noise in the notifications as i work through this ticket
comment:14 by , 5 months ago
| Patch needs improvement: | unset |
|---|
comment:15 by , 4 months ago
| Patch needs improvement: | set |
|---|
While I think I've imported from
django.template.defaultfiltersin 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 fromdjango.utils.*, e.g. theslugifyfilter usesdjango.utils.text.slugify. I don't mind linking to those utility functions from the template tag documentation.