#28999: URL Reverse does not work for CBV (Class Based Views)
-----------------------------------------+---------------------------------
Reporter: airstandley | Owner: nobody
Type: Bug | Status: new
Component: Generic views | Version: 2.0
Severity: Normal | Keywords: url reverse cbv
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+---------------------------------
This is primarily an issue with CBV {{{get_view()}}} method.
However this affects basically all url reverse resolution as urls.base
{{{reverse()}}} -> urls.resolvers {{{get_resolver()}}} -> urls.resolvers
**RegexURLResolver** -> utils.datastructures **MultiValueDict** -> dict()
{{{__getitem__()}}} which uses the key's hash value.
I discovered this while attempting to reverse a class based view. No
matter what I tried I could not get a reverse url. Quite a bit of testing
and digging later, the issue appears to be that {{{as_view()}} creates a
new view function on every call, and every one of these functions has a
unique hash. This means that using the result of one call to "as_view" as
the key in **MultiValueDict** results in a value you can not retrieve with
a subsequent call.
{{{
from testapp.views import MyCBView
from django.utils.datastructures import MultiValueDict,
MultiValueDictKeyError
lookups = MultiValueDict()
first_call = MyCBView.as_view()
second_call = MyCBView.as_view()
# Does Not Work
lookups[first_call] = "Test Retrieval"
try:
test = lookups[second_call]
except MultiValueDictKeyError:
print("Lookup Failed {} != {}".format(first_call.__hash__(),
second_call.__hash__()))
# Works
test = lookups[first_call]
print("Lookup Succeeded test={}".format(test))
}}}
I am fairly certain that it is not intended (and certainly not documented)
that you must store the return of "as_view", and use that stored value in
your urlconf, if you ever want to be able to reverse using a CBV instance.
--
Ticket URL: <https://code.djangoproject.com/ticket/28999>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/054.4823ad30fa7681411049d0d7377dd677%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.