#30995: Feature/docs: how should url converters decline to match for a named
route?
-------------------------------------+-------------------------------------
Reporter: Jack Cushman | Owner: nobody
Type: New feature | Status: new
Component: Core (URLs) | Version: master
Severity: Normal | Resolution:
Keywords: reverse, urls, | Triage Stage: Accepted
converter |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jack Cushman):
''Is your second "Resolution based on values" example right? It should be
number of positional args, rather then type, no?''
Yeah, just for the sake of documenting this, the actual logic of
{{{reverse()}}} is a little more sophisticated than the docs suggest.
Reversing by name returns the last-defined url with the given name, where
(a) the arg count or kwarg keywords match, and (b) the generated url
matches the path or re_path's regex. The second test means that we match
based on argument type as well as count in practice. While matching on
type isn't documented (that I know of), I think it's implicit to the user
expectation that {{{reverse()}}} should return URLs that resolve back to
the matched path -- otherwise in basic {{{<int:id>}}} cases you get a
weird situation where passing the wrong value to {{{reverse()}}} appears
to work but then returns a non-resolvable URL.
I hadn't noticed, but that also means that resolving based on url
converter type already works in some situations!
{{{
path('import/foo/<str:obj>', index, name='import'), # {% url "import"
"abc" %}
path('import/bar/<int:obj>', index, name='import'), # {% url "import"
123 %}
}}}
The above works because {{{IntConverter}}} happens to follow the
undocumented protocol that converters can decline a match by returning a
string that doesn't match their regex, so reversing on "abc" falls through
to the previous route.
''Then, yes, your suggestion to raise a ValueError seems plausible enough.
Want to take that on?''
Sure! Haven't contributed before, but the contributor docs look great --
I'll take a pass at it and see if I can figure it out.
--
Ticket URL: <https://code.djangoproject.com/ticket/30995#comment:4>
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/066.73a7845c475c7912a1a7cbd7141246d4%40djangoproject.com.