Code

Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#2564 closed defect (invalid)

urlresolvers.reverse broken when reversing decorated views

Reported by: dcwatson@… Owned by: jacob
Component: Core (Cache system) Version: master
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: UI/UX:

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...

Attachments (0)

Change History (8)

comment:1 Changed 8 years ago by anonymoustest

  • Cc test added
  • Keywords decoratortest added; decorator removed
  • Summary changed from urlresolvers.reverse broken when reversing decorated views to urlresolvers.reverse broken when reversing decorated viewstest

comment:2 Changed 8 years ago by adrian

#2713 was a duplicate.

comment:3 Changed 7 years ago by anonymous

  • Cc test removed
  • Keywords decorator added; decoratortest removed
  • Summary changed from urlresolvers.reverse broken when reversing decorated viewstest to urlresolvers.reverse broken when reversing decorated views

comment:4 Changed 7 years ago by SmileyChris

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 Changed 7 years ago by SmileyChris

  • Resolution set to invalid
  • Status changed from new to 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:6 Changed 7 years ago by SmileyChris

New ticket: #4919

comment:7 Changed 7 years ago by mtredinnick

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 Changed 7 years ago by SmileyChris

(and the ticket was #4149 sorry)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.