File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -631,6 +631,40 @@ Note it is also possible to extend a template rather than copy it, and override
631
631
632
632
#### Creating a custom filter
633
633
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
+ ```
634
668
635
669
#### Creating a custom widget/template
636
670
You can’t perform that action at this time.
0 commit comments