#35244 closed New feature (duplicate)
URL resolution does not work with a namespaced function reference.
Reported by: | Willem Van Onsem | Owned by: | nobody |
---|---|---|---|
Component: | Core (URLs) | Version: | 5.0 |
Severity: | Normal | Keywords: | urls, resolve, reverse, resolution |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
First mentioned on StackOverflow with: https://stackoverflow.com/questions/78035103/in-django-is-it-possible-to-use-reverse-redirect-with-a-view-function-not-stri
This is because the .reverse_dict
dictionary is not populated with view functions, or at least not if there is an app_name
around line 563 of resolvers.py
: https://github.com/django/django/blob/6feaad9113fd38ba3970032d2b7856c77403e29e/django/urls/resolvers.py#L563-L567
A patch is quite simple: look in the .reverse_dict
of the subpath, and propagate the values to the upper level in case these are callable (see patch).
But the question is of course if this approach is sound and complete.
Attachments (1)
Change History (3)
by , 10 months ago
Attachment: | resolver_view_function.patch added |
---|
comment:1 by , 10 months ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Summary: | URL resolution does not work when giving a function reference if it is listed in `urls` with `app_name` → URL resolution does not work with a namespaced function reference. |
Type: | Uncategorized → New feature |
comment:2 by , 10 months ago
It is indeed discouraged, but the fact that app_name
"breaks" it, is a bit of an "admission of weakness". Indeed, there are Django apps that could use reverse and plug in a view function reference. Now if one would use such Django app, and then later include it in urls.py
with an app_name
, the problems start occurring, so either it makes sense to disable passing view function references at all, or these have to span over the entire app.
The patch is just some fast prototyping, I think the main problem is "proving" its soundness and completeness, which is of course difficult, given the nice Christmas tree that building the reverse dictionary has become :).
In the future, please include a complete ticket description and don't force reviewers to investigate what you actually want to report. You also didn't include any test which makes it even more difficult to understand what you want to accomplish.
As far as I'm aware it's about passing a view to the
reverse()
which is officially discouraged in the docs (especially for namespaced views).Duplicate of #17914.