[Django] #16391: New URL tag for reversing urls with placeholder args/kwargs

45 views
Skip to first unread message

Django

unread,
Jul 2, 2011, 12:05:37 PM7/2/11
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
----------------------------+------------------------------
Reporter: h.a.clifford@… | Owner: nobody
Type: New feature | Status: new
Milestone: | Component: Core (Other)
Version: 1.3 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------+------------------------------
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>

--
Ticket URL: <https://code.djangoproject.com/ticket/16391>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 7, 2011, 7:23:09 AM7/7/11
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
h.a.clifford@… | Status: new
Type: New | Component: Core (Other)
feature | Severity: Normal
Milestone: | Keywords:
Version: 1.3 | Has patch: 0
Resolution: | Needs tests: 0
Triage Stage: Design | Easy pickings: 0
decision needed |
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* needs_docs: => 0
* stage: Unreviewed => Design decision needed
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

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+)(?: %>)'` or
`r'/users/(\w+|<% \w+ %>)'` in your URLconf — sure, it's a hack.

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:1>

Django

unread,
Aug 12, 2011, 9:35:38 AM8/12/11
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
h.a.clifford@… | Status: new
Type: New | Component: Core (Other)
feature | Severity: Normal
Milestone: | Keywords:
Version: 1.3 | Has patch: 0
Resolution: | Needs tests: 0
Triage Stage: Design | Easy pickings: 0
decision needed |
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------

Comment (by jezdez):

This sounds like a great idea for a 3rd party app, that would provide
helpers for JavaScript development.

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:2>

Django

unread,
Aug 12, 2011, 9:46:17 AM8/12/11
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
h.a.clifford@… | Status: new
Type: New | Component: Core (Other)
feature | Severity: Normal
Milestone: | Keywords:
Version: 1.3 | Has patch: 0
Resolution: | Needs tests: 0
Triage Stage: Design | Easy pickings: 0
decision needed |
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
Description changed by ramiro:

Old description:

> 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>

New description:

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>
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:3>

Django

unread,
Aug 17, 2011, 7:18:44 PM8/17/11
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
h.a.clifford@… | Status: new
Type: New | Component: Core (Other)
feature | Severity: Normal
Milestone: | Keywords:
Version: 1.3 | Has patch: 0
Resolution: | Needs tests: 0
Triage Stage: Design | Easy pickings: 0
decision needed |
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------

Comment (by anonymous):

To follow up on this, would also be useful with URI templates:

http://code.google.com/p/uri-templates/
http://bitworking.org/projects/URI-Templates

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:4>

Django

unread,
Sep 26, 2011, 6:57:49 PM9/26/11
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
------------------------------------------+------------------------------
Reporter: h.a.clifford@… | Owner: nobody
Type: New feature | Status: new
Milestone: | Component: Core (Other)
Version: 1.3 | Severity: Normal
Resolution: | Keywords:
Triage Stage: Someday/Maybe | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------------
Changes (by jacob):

* stage: Design decision needed => Someday/Maybe


Comment:

I agree with jezdez - this is something that'd be best done in a third-
party app. I'm not going to close this ticket, but I am going to stick it
in "someday/maybe" purgatory. I think it's a good idea, but the way to get
this in Django is to start with a solid third-party implementation and
then push to get that in.

Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:5>

Django

unread,
Jul 20, 2012, 12:19:27 AM7/20/12
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
--------------------------------+-----------------------------------------
Reporter: h.a.clifford@… | Owner: nobody
Type: New feature | Status: new
Component: Core (Other) | Version: 1.3
Severity: Normal | Resolution:
Keywords: | Triage Stage: Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------
Changes (by mitar):

* cc: mmitar@… (added)


Comment:

Without the support in `reverse` which would allow caller to skip regex
check (but the default would of course still be to check it) it would be
hard to implement such a tag (which I am also missing) without duplicating
code (and thus breaking the DRY principle).

+1 for at least support for `reverse` to have an argument to skip regex
check. Then tag is simple to implement.

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:6>

Django

unread,
Jul 20, 2012, 12:50:20 AM7/20/12
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
--------------------------------+-----------------------------------------
Reporter: h.a.clifford@… | Owner: nobody
Type: New feature | Status: new
Component: Core (Other) | Version: 1.3
Severity: Normal | Resolution:
Keywords: | Triage Stage: Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------

Comment (by mitar):

Maybe I am wrong. This simple version works quite nicely:

{{{
@register.simple_tag
def urltemplate(viewname):
urlconf = urlresolvers.get_urlconf()
resolver = urlresolvers.get_resolver(urlconf)
prefix = urlresolvers.get_script_prefix()

possibilities = resolver.reverse_dict.getlist(viewname)

if len(possibilities) != 1:
# TODO: Template tags should not the throwing exceptions (only if
TEMPLATE_DEBUG is true)
raise NotImplementedError

possibility, pattern, defaults = possibilities[0]

if len(possibility) != 1:
# TODO: Template tags should not the throwing exceptions (only if
TEMPLATE_DEBUG is true)
raise NotImplementedError

result, params = possibility[0]

for param in params:
result = result.replace('%%(%s)s' % param, '{%s}' % param)

return prefix + result
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:7>

Django

unread,
Sep 29, 2012, 8:20:47 PM9/29/12
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
--------------------------------+-----------------------------------------

Reporter: h.a.clifford@… | Owner: nobody
Type: New feature | Status: new
Component: Core (Other) | Version: 1.3
Severity: Normal | Resolution:
Keywords: | Triage Stage: Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------

Comment (by mitar):

I add this code (with some improvements) [http://django-
missing.readthedocs.org/en/latest/templatetags.html#missing.templatetags.url_tags.urltemplate
to my library].

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:8>

Django

unread,
Mar 14, 2013, 1:34:46 PM3/14/13
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
--------------------------------+-----------------------------------------

Reporter: h.a.clifford@… | Owner: nobody
Type: New feature | Status: new
Component: Core (URLs) | Version: 1.3

Severity: Normal | Resolution:
Keywords: | Triage Stage: Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------
Changes (by aaugustin):

* component: Core (Other) => Core (URLs)


Comment:

#2615 was a duplicate.

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:9>

Django

unread,
Oct 27, 2015, 11:06:57 AM10/27/15
to django-...@googlegroups.com
#16391: New URL tag for reversing urls with placeholder args/kwargs
--------------------------------+-----------------------------------------
Reporter: h.a.clifford@… | Owner: nobody
Type: New feature | Status: closed

Component: Core (URLs) | Version: 1.3
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => wontfix


Comment:

I haven't heard of much demand for this feature, so living as a third-
party library seems okay to me.

--
Ticket URL: <https://code.djangoproject.com/ticket/16391#comment:10>

Reply all
Reply to author
Forward
0 new messages