#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: | no | UI/UX: | no |
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)
by , 20 years ago
| Attachment: | random_tag.patch added |
|---|
comment:1 by , 20 years ago
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! :)
by , 20 years ago
| Attachment: | random_tag_weighted.patch added |
|---|
Implements random template tag, with optional weighting.
by , 20 years ago
| Attachment: | random_tag_weighted_newstyle.patch added |
|---|
Implements random template tag, with optional weighting, updated for new-style tag registration.
comment:3 by , 20 years ago
Added an updated patch to take into account new-style tag registration introduced in [1443].
by , 20 years ago
| Attachment: | random_tag_weighted_newstyle_fixed.patch added |
|---|
Implements random template tag, fixed for bisect / randint bug.
comment:4 by , 20 years ago
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.
by , 20 years ago
| Attachment: | random_tag_current.diff added |
|---|
Implements random template tag; current version (will overwrite with future updates).
comment:5 by , 20 years ago
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 by , 20 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
This is out of the scope of the built-in tag set (but still kinda cool).
comment:7 by , 18 years ago
| Reporter: | changed from to |
|---|
comment:8 by , 16 years ago
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
randomtemplate tag