1 |
|
---|
2 | Index: django/template/defaultfilters.py
|
---|
3 | ===================================================================
|
---|
4 | --- django/template/defaultfilters.py (revision 2928)
|
---|
5 | +++ django/template/defaultfilters.py (working copy)
|
---|
6 | @@ -246,6 +246,13 @@
|
---|
7 | "Returns a random item from the list"
|
---|
8 | return random_module.choice(value)
|
---|
9 |
|
---|
10 | +def get(value, arg):
|
---|
11 | + "Returns an item from an idexable item"
|
---|
12 | + try:
|
---|
13 | + return value[arg]
|
---|
14 | + except LookupError:
|
---|
15 | + return value # Fail silently.
|
---|
16 | +
|
---|
17 | def slice_(value, arg):
|
---|
18 | """
|
---|
19 | Returns a slice of the list.
|
---|
20 | @@ -473,6 +480,7 @@
|
---|
21 | register.filter(removetags)
|
---|
22 | register.filter(random)
|
---|
23 | register.filter(rjust)
|
---|
24 | +register.filter(get)
|
---|
25 | register.filter('slice', slice_)
|
---|
26 | register.filter(slugify)
|
---|
27 | register.filter(stringformat)
|
---|
28 | Index: tests/othertests/defaultfilters.py
|
---|
29 | ===================================================================
|
---|
30 | --- tests/othertests/defaultfilters.py (revision 2928)
|
---|
31 | +++ tests/othertests/defaultfilters.py (working copy)
|
---|
32 | @@ -173,6 +173,30 @@
|
---|
33 | >>> length_is('a', 10)
|
---|
34 | False
|
---|
35 |
|
---|
36 | +>>> get([1, 2, 3], 0)
|
---|
37 | +1
|
---|
38 | +
|
---|
39 | +>>> get([1, 2, 3], 1)
|
---|
40 | +2
|
---|
41 | +
|
---|
42 | +>>> get([1, 2, 3], 2)
|
---|
43 | +3
|
---|
44 | +
|
---|
45 | +>>> get([1, 2, 3], 3)
|
---|
46 | +[1, 2, 3]
|
---|
47 | +
|
---|
48 | +>>> get({'a': 1, 'c': 3, 'e': 5}, 'a')
|
---|
49 | +1
|
---|
50 | +
|
---|
51 | +>>> get({'a': 1, 'c': 3, 'e': 5}, 'c')
|
---|
52 | +3
|
---|
53 | +
|
---|
54 | +>>> get({'a': 1, 'c': 3, 'e': 5}, 'e')
|
---|
55 | +5
|
---|
56 | +
|
---|
57 | +>>> get({'a': 1, 'c': 3, 'e': 5}, 'g')
|
---|
58 | +{'a': 1, 'c': 3, 'e': 5}
|
---|
59 | +
|
---|
60 | >>> slice_('abcdefg', '0')
|
---|
61 | ''
|
---|