Opened 13 years ago
Last modified 9 years ago
#16391 closed New feature
New URL tag for reversing urls with placeholder args/kwargs — at Version 3
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Core (URLs) | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | mmitar@… | Triage Stage: | Someday/Maybe |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
It would be incredibly useful if Django supported a placeholder url tag:
usage would be similar to url, but with args/kwargs that don't match the url regex.
E.g: {% urlplaceholder User username='<%username%>' %}
, where my url rule only allows a-z usernames.
(Currently, you can't do this).
This isn't a huge change which is why I'm suggesting it (line 325 urlresolves does the regex check after substituting in the values)
Why is this useful? It would allow *easy* generation of javascript templates on the frontend - all the current methods seem to involve reversing the urls with javasacript which is hardly in keeping DRY.
Example:
<script id='UserTemplate' type='jqueryTemplate'> <a href='{% urlplaceholder User username='<%username%>' %}'> <img src='{% get_media_url %}/user.png'> </a> </script>
Change History (3)
comment:1 by , 13 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:2 by , 13 years ago
This sounds like a great idea for a 3rd party app, that would provide helpers for JavaScript development.
comment:3 by , 13 years ago
Description: | modified (diff) |
---|
I wouldn't dismiss the regex check so quickly. Currently, there's a guarantee that an URL generated with
reverse
will be resolved correctly. While I understand your reasons to drop this guarantee, we can't do this lightly.Note that if #16362 was committed, it would rely on the regex check after substituting the values. Those two tickets are incompatible.
You could use a something like
r'/users/(?:<% )(\w+)(?: %>)'
orr'/users/(\w+|<% \w+ %>)'
in your URLconf — sure, it's a hack.EDIT: while working on this, I missed the fact that the ticket suggested a new tag, not a modification of the existing tag. Please disregard the comment above. Still, it's DDN.