Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#2564 closed defect (invalid)

urlresolvers.reverse broken when reversing decorated views

Reported by: dcwatson@… Owned by: Jacob
Component: Core (Cache system) Version: dev
Severity: normal Keywords: urlresolvers reverse cache decorator
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I noticed when trying to use urlresolvers.reverse, if you pass a decorated function, you get the following error:

Tried test in module testproj.views. Error was: 'function' object has no attribute 'method'

Here's a simple setup to reproduce the error:

urls.py:

from django.conf.urls.defaults import *
urlpatterns = patterns('',
    ( r'^test/$', 'testproj.views.test' ),
    ( r'^test/(?P<section>.+)/$', 'testproj.views.sect' ),
)

views.py:

from django.core import urlresolvers
from django.views.decorators.cache import cache_page
from django.http import HttpResponse
def test( req ):
    s = urlresolvers.reverse( 'testproj.views.sect', kwargs={'section':'woot'} )
    return HttpResponse( s )
@cache_page( 60 )
def sect( req, section ):
    return HttpResponse( "section %s" % section )

Comment out the cache_page decorator and things work as expected...

Change History (8)

comment:1 by anonymoustest, 18 years ago

Cc: Smith added
Keywords: decoratortest added; decorator removed
Summary: urlresolvers.reverse broken when reversing decorated viewsurlresolvers.reverse broken when reversing decorated viewstest

comment:2 by Adrian Holovaty, 18 years ago

#2713 was a duplicate.

comment:3 by anonymous, 18 years ago

Cc: Smith removed
Keywords: decorator added; decoratortest removed
Summary: urlresolvers.reverse broken when reversing decorated viewstesturlresolvers.reverse broken when reversing decorated views

comment:4 by Chris Beaven, 18 years ago

Perhaps a solution is that all Django decorators have a property which returns the original function. Then reverse can try to use this property before falling back to the function itself.

comment:5 by Chris Beaven, 18 years ago

Resolution: invalid
Status: newclosed

This ticket is actually user error, the problem is that cache_page can't be used with Python 2.4 decorator syntax, because it expects the view and the arguments to be passed to it at once, so you'd have to do:

def sect(req, section):
    return HttpResponse("section %s" % section)
sect = cache_page(sect, 60)

This is actually a documentation bug (or the decorator format needs to change), but I'll submit a new ticket for that.

comment:6 by Chris Beaven, 18 years ago

New ticket: #4919

comment:7 by Malcolm Tredinnick, 18 years ago

This isn't a documentation error, it's a long-standing bug in the cache_page decorator. It's entirely consistent that it shoudl be able to be used as a decorator, it just doesn't have the subtlety in the implementation that some of our other decorators have to work like this.

There was/is a ticket open about this somewhere, but I can't find it right at the moment.

comment:8 by Chris Beaven, 18 years ago

(and the ticket was #4149 sorry)

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