#30995: Feature/docs: how should url converters decline to match for a named
route?
-------------------------------------+-------------------------------------
Reporter: jcushman | Owner: nobody
Type: New | Status: new
feature |
Component: Core | Version: 2.2
(URLs) | Keywords: reverse, urls,
Severity: Normal | converter
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
It is sometimes convenient to have multiple instances of a named route,
where the correct one is chosen based on whether the URL converters
`to_url` call succeeds. For example, the attached file has routes like
this:
{{{
path('export/foo/<foo:obj>', index, name='export'),
path('export/bar/<bar:obj>', index, name='export'),
}}}
I then wanted to put {{{ {% url "export" some_foo_or_bar %} }}} in a
generic export template and have the correct URL inserted.
My first attempt to do this was to raise {{{ValueError}}} in {{{to_url}}}
for non-matching values, hoping that it would work the same as
{{{to_python}}} where the docs specify that "A ValueError is interpreted
as no match."
That didn't work -- nothing catches the ValueError in {{{to_url}}} -- so I
found the workaround demonstrated in the attachment: if {{{to_url}}}
returns an empty string (or some string that doesn't match the converter's
regex), then the later regex check in {{{_reverse_with_prefix}}} will
detect the non-match and everything seems to work out.
So this is either a feature request or a documentation check. I'm thinking
either:
* {{{_reverse_with_prefix}}} could be updated so {{{to_url}}} works the
same as {{{to_python}}}, and a {{{ValueError}}} indicates no match (which
I think just means wrapping {{{try: ... except ValueError: continue}}}
around the appropriate line), or
* the docs could be updated to indicate that, in {{{to_url}}}, a converter
should return a non-regex-matching string such as the empty string in
order to decline a match.
--
Ticket URL: <https://code.djangoproject.com/ticket/30995>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.fc6b72eda3abd38cecbedca11ffb9e43%40djangoproject.com.