#2564 closed defect (invalid)
urlresolvers.reverse broken when reversing decorated views
Reported by: | 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 , 18 years ago
Cc: | added |
---|---|
Keywords: | decoratortest added; decorator removed |
Summary: | urlresolvers.reverse broken when reversing decorated views → urlresolvers.reverse broken when reversing decorated viewstest |
comment:2 by , 18 years ago
comment:3 by , 18 years ago
Cc: | removed |
---|---|
Keywords: | decorator added; decoratortest removed |
Summary: | urlresolvers.reverse broken when reversing decorated viewstest → urlresolvers.reverse broken when reversing decorated views |
comment:4 by , 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 , 18 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
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:7 by , 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.
#2713 was a duplicate.