#15794 closed Bug (fixed)
csrf_exempt decorator applied to http method in class based view - broken
| Reported by: | Mike Fogel | Owned by: | nobody |
|---|---|---|---|
| Component: | Documentation | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Decorating anything other than the dispatch() method of a class based view with csrf_exempt doesn't work. For example:
class MyView(FormView):
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(MyView, self).dispatch(*args, **kwargs)
def post(self, request, *args, **kwargs):
# ....
return super(MyView, self).post(request, *args, **kwargs)
works.
class MyView(FormView):
def dispatch(self, *args, **kwargs):
return super(MyView, self).dispatch(*args, **kwargs)
@method_decorator(csrf_exempt)
def post(self, request, *args, **kwargs):
# ....
return super(MyView, self).post(request, *args, **kwargs)
does not work. This returns a 403 - CSRF verification failed.
This is because the as_view() function in [source:django/trunk/django/views/generic/base.py#L54] only carries the __dict__ from the dispatch() method forward - not those of post(), get(), etc.
The documentation here [source:django/trunk/docs/topics/class-based-views.txt#L590] claims that csrf_exempt can be applied to the http method names.
Attached is a trivial documentation patch.
Attachments (1)
Change History (4)
by , 15 years ago
| Attachment: | documentation.diff added |
|---|
comment:1 by , 15 years ago
| Component: | Generic views → Documentation |
|---|---|
| Triage Stage: | Unreviewed → Ready for checkin |
I agree that we should fix the documentation to say that you should decorate the
dispatchmethod. In some cases, it may be possible to decorate theget/post/etc methods etc. but that depends on the nature of the decorator.It would be possible to 'fix' this by copying attributes from the
get/post/etc methods but that is problematic - we can't actually enumerate what all those methods are for a start, and this approach is only going to work for some types of decorators, and otherwise cause confusing and inconsistent behaviour.