Code

Opened 6 years ago

Closed 6 years ago

#5920 closed (invalid)

Error AttributeError: 'function' object has no attribute 'status_code' in 6668 version

Reported by: aaloy Owned by: ramiro
Component: Core (Cache system) Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This error appears in the last trunk, it not appears in 6473 version and I don't get it in all pages.

Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 278, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 620, in __call__
    return self.application(environ, start_response)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 209, in __call__
    response = middleware_method(request, response)
  File "/usr/lib/python2.5/site-packages/django/middleware/gzip.py", line 14, in process_response
    if response.status_code != 200 or len(response.content) < 200:

Attachments (0)

Change History (9)

comment:1 Changed 6 years ago by aaloy

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

It seems the issue is not with http but with the cache_page decorator. The decorator worked with the previos version and does not work with the trunk versions.

This worke on version 6473

def ajax_zonas(request):
    """Get all ojects from table Provincia odrdereb by name"""
    listado = Provincia.objects.all().order_by('name')
    json = serialize('json', listado,ensure_ascii=True)
    return  HttpResponse(json, mimetype='text/javascript')
ajax_zonas = cache_page(ajax_zonas, 60)

comment:2 Changed 6 years ago by the daniel <danielwh@…>

Hmm, the cache_page decorator is working for me in trunk (r6826) - are there more details to reproduce? What middleware do you have installed in what order, what's an example of a view that does work correctly, etc?

comment:3 Changed 6 years ago by guettli

  • Resolution set to invalid
  • Status changed from new to closed

No feedback since several months. Closing it, since some people said 'worksforme'.

Please reopen if problem still happens with current SVN trunk.

comment:4 Changed 6 years ago by MihaiD

  • Component changed from Uncategorized to Cache system
  • Resolution invalid deleted
  • Status changed from closed to reopened

This still doesn't seem to work in the svn trunk. I get the following error:

Traceback (most recent call last):

File "G:\Python25\lib\site-packages\django\core\servers\basehttp.py", line 277, in run

self.result = application(self.environ, self.start_response)

File "G:\Python25\lib\site-packages\django\core\servers\basehttp.py", line 631, in call

return self.application(environ, start_response)

File "G:\Python25\lib\site-packages\django\core\handlers\wsgi.py", line 209, in call

response = middleware_method(request, response)

File "G:\Python25\lib\site-packages\django\middleware\common.py", line 90, in process_response

if response.status_code == 404:

AttributeError: 'function' object has no attribute 'status_code'

I use cache_page in urls.py. Using @cache_page in views seems to work though (or at least no errors were generated).
I have the following middleware set:

MIDDLEWARE_CLASSES = (

'django.middleware.cache.CacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',

)

I couldn't determine if CacheMiddleware was needed for individual views or not so I've tested with and without it, with the same result.

comment:5 Changed 6 years ago by MihaiD

  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 6 years ago by ramiro

  • Owner changed from nobody to ramiro
  • Status changed from reopened to new

comment:7 Changed 6 years ago by ramiro

  • Status changed from new to assigned

@MihaiD: I have been able to reproduce the problem by using cache_page in the URLconf as you describe but by using the string syntax for the view being "decorated" instead of the callable syntax (see http://www.djangoproject.com/documentation/release_notes_0.96/#urlconf-improvements). This confuses django.utils.decorators.decorator_from_middleware() than then ends pushing a stub function as a decorator/middleware method parameter in a place where a HttpResponse is expected.

import views

urlpatterns = patterns('',
    (r'^foo/$', cache_page('myproj.myapp.views.myview')), # This is WRONG
    (r'^foo/$', cache_page(views.myview)), # This is OK
)

Can you confirm this is what you are doing in your setup?. If so, I will then change Component field of this ticket to Documentation and will submit a patch against the URL dispatcher docs to clarify this.

comment:8 Changed 6 years ago by MihaiD

Yep, that's what I was trying to do. Work's fine with the second syntax you wrote

comment:9 Changed 6 years ago by ramiro

  • Resolution set to invalid
  • Status changed from assigned to closed

The OP didn't provide enough information so we can't say for sure if the problem is the same one that got diagnosed (other possible causes could have been some bugs the decorator from middleware stuff had that were polished as time passed), but a a second reporter with the same symptoms did. He was using a decorator in a wrong way. Closing this ticket and opening #6832 to clarify the usage case description in the documentation.

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.