#919 closed enhancement (wontfix)
[patch] Implement `random` template tag
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Template system | Version: | |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | UI/UX: |
Description
This patch implements a random
template tag, doing for tag blocks what the random
filter does for list items.
A random block begins with random
and ends with endrandom
; items are separated with or
tags. Each possible tag block has an equal possibility of being selected, just as with the random
filter.
I'm not sure or
is an optimal separator tag for the random choices, but it was the best I've been able to come up with; suggestions for a superior tag name would be greatly appreciated.
I'm looking to additionally implement a "weighted" random option, which will allow one to specify unequal possibilities for random selection -- e.g., of two choices, the first is selected four times more often than the second. This will require a helper class, but I'm not certain which module it belongs in (perhaps django.core.extensions
?).
Attachments (5)
Change History (13)
Changed 12 years ago by
Attachment: | random_tag.patch added |
---|
comment:1 Changed 12 years ago by
Silly me; I realized that I don't need a helper class to implement the weighted random option, as the logic could just go in RandomNode
-- although such a helper class might make it easier to implement such weighted random selection in multiple places (e.g., in the random
filter as well). Either way, I'm still curious as to where such a thing would go! :)
Changed 12 years ago by
Attachment: | random_tag_weighted.patch added |
---|
Implements random
template tag, with optional weighting.
comment:2 Changed 12 years ago by
Added a modified patch that implements optional weighted selection.
Changed 12 years ago by
Attachment: | random_tag_weighted_newstyle.patch added |
---|
Implements random
template tag, with optional weighting, updated for new-style tag registration.
comment:3 Changed 12 years ago by
Added an updated patch to take into account new-style tag registration introduced in [1443].
Changed 12 years ago by
Attachment: | random_tag_weighted_newstyle_fixed.patch added |
---|
Implements random
template tag, fixed for bisect / randint bug.
comment:4 Changed 12 years ago by
In further testing, I realized that the weighted distribution was off; it turns out I had assumed that random.randint
was exclusive of its upper bound, whereas it is inclusive of both bounds. Furthermore, the proper bisect
function is bisect_right
, not bisect_left
.
Changed 12 years ago by
Attachment: | random_tag_current.diff added |
---|
Implements random
template tag; current version (will overwrite with future updates).
comment:5 Changed 12 years ago by
Added a check for the optional weights to ensure they are positive integers.
All further patch updates will be overwrites of random_tag_current.diff
; I didn't realize the clutter I was creating. :-)
comment:6 Changed 12 years ago by
Resolution: | → wontfix |
---|---|
Status: | new → closed |
This is out of the scope of the built-in tag set (but still kinda cool).
comment:7 Changed 11 years ago by
Reporter: | changed from Tom Tobin <korpios@…> to Tom Tobin <korpios@…> |
---|
comment:8 Changed 9 years ago by
Hi,
Just for the record: With Django 1.1, the code only worked here after I removed the line 681: parser.delete_first_token()
Thanks for the code!
Implements
random
template tag