Opened 19 years ago

Closed 18 years ago

Last modified 17 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

Description

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/base.py", line 64, in get_response
    response = callback(request, **param_dict)

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

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

  File "/usr/local/lib/python2.4/site-packages/django/core/template.py", line 436, in render
    bits.append(node.render(context))

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

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

  File "/usr/local/lib/python2.4/site-packages/django/core/defaultfilters.py", 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 by anonymous, 19 years ago

Patch:

===================================================================
--- core/defaultfilters.py      (revision 537)
+++ core/defaultfilters.py      (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 by Boffbowsh, 19 years ago

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

comment:3 by Marc Fargas <telenieko@…>, 18 years ago

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 by anonymous, 18 years ago

Cc: paul.bowsher@… removed

Removed self from CC

comment:5 by Gary Wilson <gary.wilson@…>, 18 years ago

was fixed in [538].

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