Opened 14 years ago

Closed 12 years ago

Last modified 12 years ago

#371 closed defect (fixed)

random filter in template throws an error

Reported by: Boffbowsh Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Using {{loopimgs|random}} in a template throws the following error:

There's been an error:

Traceback (most recent call last):

  File "/usr/local/lib/python2.4/site-packages/django/core/handlers/", line 64, in get_response
    response = callback(request, **param_dict)

  File "/home/django/www/apps/browse/views/", line 14, in index
    return HttpResponse(t.render(c))

  File "/usr/local/lib/python2.4/site-packages/django/core/", line 115, in render
    return self.nodelist.render(context)

  File "/usr/local/lib/python2.4/site-packages/django/core/", line 436, in render

  File "/usr/local/lib/python2.4/site-packages/django/core/", line 466, in render
    output = resolve_variable_with_filters(self.var_string, context)

  File "/usr/local/lib/python2.4/site-packages/django/core/", line 411, in resolve_variable_with_filters
    obj = registered_filters[name][0](obj, arg)

  File "/usr/local/lib/python2.4/site-packages/django/core/", line 235, in random
    return random.choice(value)

AttributeError: 'function' object has no attribute 'choice'

loopimgs definately contains items, the var on it's own produces this:
[Baby_Cube.jpg, kids-hiding.jpg, kids2.jpg, girl_car.jpg, kids.jpg, kidsball.jpg]

Change History (5)

comment:1 Changed 14 years ago by anonymous


--- core/      (revision 537)
+++ core/      (working copy)
@@ -1,6 +1,7 @@
 "Default variable filters"

-import template, re, random
+import template, re
+import random as random_module

 # STRINGS         #
@@ -232,7 +233,7 @@

 def random(value, _):
     "Returns a random item from the list"
-    return random.choice(value)
+    return random_module.choice(value)

 def slice_(value, arg):

comment:2 Changed 14 years ago by Boffbowsh

Patch contributed by me. Must make a login...

comment:3 Changed 12 years ago by Marc Fargas <telenieko@…>

Has patch: set
Resolution: fixed
Status: newclosed

The patch is no longer valid as things have moved to another file, but the change it suggests has already been done so I assume that bug is solved. If not, please reopen.

comment:4 Changed 12 years ago by anonymous

Cc: paul.bowsher@… removed

Removed self from CC

comment:5 Changed 12 years ago by Gary Wilson <gary.wilson@…>

was fixed in [538].

Note: See TracTickets for help on using tickets.
Back to Top