| 645 | | When you've written your filter definition, you need to register it with |
|---|
| | 645 | Template filters which expect strings |
|---|
| | 646 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 647 | |
|---|
| | 648 | If you're writing a template filter which only expects a string as the first |
|---|
| | 649 | argument, you should use the included decorator ``stringfilter``. This will |
|---|
| | 650 | convert an object to it's string value before being passed to your function:: |
|---|
| | 651 | |
|---|
| | 652 | from django.template.defaultfilters import stringfilter |
|---|
| | 653 | |
|---|
| | 654 | @stringfilter |
|---|
| | 655 | def lower(value): |
|---|
| | 656 | return value.lower() |
|---|
| | 657 | |
|---|
| | 658 | Registering a custom filters |
|---|
| | 659 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 660 | |
|---|
| | 661 | Once you've written your filter definition, you need to register it with |
|---|
| 665 | | def lower(value): |
|---|
| 666 | | return value.lower() |
|---|
| 667 | | |
|---|
| 668 | | If you leave off the ``name`` argument, as in the second example above, Django |
|---|
| 669 | | will use the function's name as the filter name. |
|---|
| 670 | | |
|---|
| 671 | | Template filters which expect strings |
|---|
| 672 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 673 | | If you are writing a template filter which only expects a string as the first |
|---|
| 674 | | argument, you should use the included decorator ``stringfilter`` which will convert |
|---|
| 675 | | an object to it's string value before being passed to your function:: |
|---|
| 676 | | |
|---|
| 677 | | from django.template.defaultfilters import stringfilter |
|---|
| 678 | | |
|---|