Opened 9 years ago

Closed 9 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 Mattia Procopio, 9 years ago

can you add your use case please?

comment:2 by Rik, 9 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 weird_usecase():
    return SimpleLazyObject(lambda: HttpResponseRedirect(reverse('name')))

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.

Last edited 9 years ago by Rik (previous) (diff)

comment:3 by Rik, 9 years ago

Cc: gitaarik@… added

comment:4 by Tim Graham, 9 years ago

Resolution: needsinfo
Status: newclosed

Closing as "needs info" in absence of a use case and/or confirmation that the related ticket won't solve this.

Note: See TracTickets for help on using tickets.
Back to Top