Comments (4)
Can you include a stack trace? I hadn't tested with class-based views previously; I suspect there might be something funny going on with the decoration.
from django-jimmypage.
Thanks for your time!
Traceback:
File "/Users/tm/.virtualenvs/vidproject/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
117. response = middleware_method(request, e)
File "/Users/tm/.virtualenvs/vidproject/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/tm/.virtualenvs/vidproject/lib/python2.6/site-packages/django/views/generic/base.py" in view
47. return self.dispatch(request, *args, **kwargs)
File "/Users/tm/.virtualenvs/vidproject/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapper
28. return bound_func(*args, **kwargs)
File "/Users/tm/.virtualenvs/vidproject/src/django-jimmypage/jimmypage/cache.py" in decorated
110. cache.set(key, response.content, self.time)
File "/Users/tm/.virtualenvs/vidproject/lib/python2.6/site-packages/django/template/response.py" in _get_content
110. raise ContentNotRenderedError('The response content must be rendered before it can be accessed.')
Exception Type: ContentNotRenderedError at /vid/10:video-2/
Exception Value: The response content must be rendered before it can be accessed.
I pushed some changes to my fork here:
https://github.com/unvs/django-jimmypage/commit/9b65185ede810e3a0e3859d40410dd5657fedd62
seems to work for now - haven't tested it with a proper memcached setup yet
from django-jimmypage.
And this is the stack trace for the first error without name = 'cache_page':
Traceback:
File "/Users/tm/.virtualenvs/vidproj/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
89. response = middleware_method(request)
File "/Users/tm/.virtualenvs/vidproj/lib/python2.6/site-packages/debug_toolbar/middleware.py" in process_request
70. original_urlconf = __import__(getattr(request, 'urlconf', settings.ROOT_URLCONF), {}, {}, ['*'])
File "/Users/tm/Sites/projects/vidproj/conf/urls.py" in <module>
13. from polsa.views import VideoDetailView, VideoListView
File "/Users/tm/Sites/projects/vidproj/apps/polsa/views.py" in <module>
17. class VideoDetailView(DetailView):
File "/Users/tm/Sites/projects/vidproj/apps/polsa/views.py" in VideoDetailView
34. @method_decorator(cache_page(60*60))
File "/Users/tm/.virtualenvs/vidproj/lib/python2.6/site-packages/django/utils/decorators.py" in method_decorator
40. update_wrapper(_dec, decorator)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/functools.py" in update_wrapper
33. setattr(wrapper, attr, getattr(wrapped, attr))
Exception Type: AttributeError at /vids/10:video-2/
Exception Value: 'cache_page' object has no attribute '__name__'
from django-jimmypage.
I created an example project with test cases for various decorator uses, and pushed it to master. This made me realize: I don't think you can properly use decorators the way you did in your example. See this bug:
https://code.djangoproject.com/ticket/13879
An alternative that I've used in some projects I've worked on is to implement an "args_method_decorator" which accepts arguments:
def args_method_decorator(decorator, *dargs, **dkwargs):
"""
Converts a function decorator into a method decorator, with arguments.
Copied from django.utils.decorators.method_decorator to allow arguments.
"""
# 'func' is a function at the time it is passed to _dec, but will eventually
# be a method of the class it is defined it.
def _dec(func):
def _wrapper(self, *args, **kwargs):
@decorator(*dargs, **dkwargs)
def bound_func(*args2, **kwargs2):
return func(self, *args2, **kwargs2)
# bound_func has the signature that 'decorator' expects i.e. no
# 'self' argument, but it is a closure over self so it can call
# 'func' correctly.
return bound_func(*args, **kwargs)
# In case 'decorator' adds attributes to the function it decorates, we
# want to copy those. We don't have access to bound_func in this scope,
# but we can cheat by using it on a dummy function.
@decorator(*dargs, **dkwargs)
def dummy(*args, **kwargs):
pass
update_wrapper(_wrapper, dummy)
# Need to preserve any existing attributes of 'func', including the name.
update_wrapper(_wrapper, func)
return _wrapper
update_wrapper(_dec, decorator)
# Change the name to aid debugging.
_dec.__name__ = 'method_decorator(%s)' % decorator.__name__
return _dec
This is invoked as:
@args_method_decorator(cache_page, 60)
def get(self, request):
...
Please feel free to reopen a ticket if you find that it's still not working properly, once the decorator usage is worked out.
from django-jimmypage.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-jimmypage.