Skip to content

Commit 623942b

Browse files
Update README.md
1 parent 0893d37 commit 623942b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,40 @@ Note it is also possible to extend a template rather than copy it, and override
631631

632632
#### Creating a custom filter
633633

634+
Create a directory for custom filters:
635+
636+
`myapp/templatetags/`
637+
638+
Create a file to hold your app filters:
639+
640+
```
641+
myapp_filters.py:
642+
643+
from django import template
644+
645+
register = template.Library()
646+
647+
@register.filter
648+
def show_root_level_folders_only(cl):
649+
"""
650+
cl is a variable holding a Change List; the list of model instances.
651+
This function filters the Change List to remove non-root elements.
652+
"""
653+
query_set = cl.result_list
654+
for folder in query_set:
655+
if folder.parent != None:
656+
query_set = query_set.exclude(pk=folder.id)
657+
cl.result_list = query_set
658+
return cl
659+
```
660+
661+
Use the filter in your template. In this example, to remove non-root elements from a list, `cl`:
662+
663+
```
664+
change_list.html:
665+
666+
{{ cl|show_root_level_folders_only }}
667+
```
634668

635669
#### Creating a custom widget/template
636670

0 commit comments

Comments
 (0)