Code

Opened 9 years ago

Closed 7 years ago

Last modified 7 years ago

#371 closed defect (fixed)

random filter in template throws an error

Reported by: Boffbowsh Owned by: adrian
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: UI/UX:

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]

Attachments (0)

Change History (5)

comment:1 Changed 9 years ago by anonymous

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 Changed 9 years ago by Boffbowsh

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

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

  • Has patch set
  • Resolution set to fixed
  • Status changed from new to closed

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

  • Cc paul.bowsher@… removed

Removed self from CC

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

was fixed in [538].

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.