3👍
The problem is that your wrapper expects “request” as the first argument, but a method on a class always takes “self” as the first argument. So in your decorator, what it thinks is the request object is actually TopSecretPage itself.
Either Vinay or artran’s solutions should work, so I won’t repeat them. Just thought a clearer description of the problem might be helpful.
6👍
The correct way to do this for any decorator applied to any class-based view method is to use django.utils.decorators.method_decorator()
. I’m not sure when method_decorator() was introduced but here is an example/update in the Django 1.2 release notes. Use it like this:
from django.utils.decorators import method_decorator
class TopSecretPage(View):
@method_decorator(custom_login)
def __call__(self, request, **kwargs):
#bla bla view stuff...
pass
- [Django]-Css not working with FastCGI and Lighttpd
- [Django]-Django multi-tenant
- [Django]-How to load fixtures.json with ManyToMany Relationships in Django
- [Django]-Gunicorn not running can't connect to sock file
- [Django]-Upgrading to django 1.11 many to many column not found
2👍
This problem has come up before. A solution is included which might work for you.
Update: Example method with decorator:
class ContentView(View):
# the thing in on_method() is the actual Django decorator
#here are two examples
@on_method(cache_page(60*5))
@on_method(cache_control(max_age=60*5))
def get(self, request, slug): # this is the decorated method
pass #in here, you access request normally
- [Django]-Postgres to Ubuntu Docker container linking not working
- [Django]-Django admin with websocket
- [Django]-Django: How to remove bullet point when printing form errors
- [Django]-Exception Value: context must be a dict rather than WSGIRequest
1👍
Instead of using the decorator on the view you could decorate the url.
For example in urls.py:
from my_decorators import myuser_login_required
from my_views import TopSecretPage
urlpatterns = patterns('',
(r'^whatever-the-url-is/$', myuser_login_required(TopSecretPage), {}),
)
You may need to play with that a little but it’s about right.
1👍
This is effectively a duplicate of Django – Correct way to pass arguments to CBV decorators?
which describes the correct way of addressing this. The correct way of doing this for django 1.9 is as follows:
@method_decorator(myuser_login_required(), name='dispatch')
class TopSecretPage(View):
..
- [Django]-How to solve "No Python interpreter configured for the module"? in Android Studio?
- [Django]-Django API Framework – Nested Models View
- [Django]-How do you produce/create tables in Geraldo Reports?
- [Django]-(Django and PIL) encoder error -2 when writing image file