Opened 10 years ago
Closed 10 years ago
#23783 closed Uncategorized (needsinfo)
Support lazy redirect_to in HttpResponseRedirectBase
Reported by: | German M. Bravo | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | 1.7 |
Severity: | Normal | Keywords: | |
Cc: | gitaarik@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
HttpResponseRedirect(reverse_lazy('name'))
fails when this object is initialized because the reverse_lazy is immediately forced to text. In some use cases it would be desirable for the object to be created and the lazy reverse be done only when it's actually needed.
Change History (4)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Regardless of the use case, your issue should be able to be solved like this:
from django.core.urlresolvers import reverse from django.http.response import HttpResponseRedirect from django.utils.functional import SimpleLazyObject def my_view(request): SimpleLazyObject(lambda: HttpResponseRedirect(reverse('core:frontpage')))
But unfortunately this doesn't work at the moment because LazyObject
(which SimpleLazyObject
implements) doesn't implement __iter__
. This is required because the BaseHandler
of wsgiref
loops through response objects, which invokes __iter__
, but if since LazyObject
doesn't implement __iter__
it falls back to __getitem__
which won't work.
I opened an issue for this:
https://code.djangoproject.com/ticket/23838
I'll make a pull request for the __iter__
method, when that will be merged, the code snipped above will work.
comment:3 by , 10 years ago
Cc: | added |
---|
comment:4 by , 10 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
Closing as "needs info" in absence of a use case and/or confirmation that the related ticket won't solve this.
can you add your use case please?