How to decorate another http method in one class based view

For example, I have a class-based view that allows you to use both the GET and POST method, as shown below,

class ViewOne(View): def post(self, request, *args, **kwargs): ... def get(self, request, *args, **kwargs): ... @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(ViewOne, self).dispatch(*args, **kwargs) 

Now, both GET and POST are login_required. But what if I want only POST to be login_required?

+7
source share
2 answers

Hm ... Doesn't work?

 class ViewOne(View): @method_decorator(login_required) def post(self, request, *args, **kwargs): ... def get(self, request, *args, **kwargs): ... 
+4
source

Why not create two classes, also use django-braces ;)

 class ViewOne(View): def get(self, request, *args, **kwargs): ... class ViewTwo(LoginRequiredMixin, ViewOne): def post(self, request, *args, **kwargs): ... 
+1
source

All Articles