|
| 1 | +title: django.views.decorators.csrf csrf_exempt Example Code |
| 2 | +category: page |
| 3 | +slug: django-views-decorators-csrf-csrf-exempt-examples |
| 4 | +sortorder: 500011516 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.views.decorators.csrf csrf_exempt |
| 7 | +meta: Python example code for the csrf_exempt callable from the django.views.decorators.csrf module of the Django project. |
| 8 | + |
| 9 | + |
| 10 | +csrf_exempt is a callable within the django.views.decorators.csrf module of the Django project. |
| 11 | + |
| 12 | + |
| 13 | +## Example 1 from django-rest-framework |
| 14 | +[Django REST Framework](https://github.com/encode/django-rest-framework) |
| 15 | +([project homepage and documentation](https://www.django-rest-framework.org/), |
| 16 | +[PyPI package information](https://pypi.org/project/djangorestframework/) |
| 17 | +and [more resources on Full Stack Python](/django-rest-framework-drf.html)), |
| 18 | +often abbreviated as "DRF", is a popular [Django](/django.html) extension |
| 19 | +for building [web APIs](/application-programming-interfaces.html). |
| 20 | +The project has fantastic documentation and a wonderful |
| 21 | +[quickstart](https://www.django-rest-framework.org/tutorial/quickstart/) |
| 22 | +that serve as examples of how to make it easier for newcomers |
| 23 | +to get started. |
| 24 | + |
| 25 | +The project is open sourced under the |
| 26 | +[Encode OSS Ltd. license](https://github.com/encode/django-rest-framework/blob/master/LICENSE.md). |
| 27 | + |
| 28 | +[**django-rest-framework / rest_framework / viewsets.py**](https://github.com/encode/django-rest-framework/blob/master/rest_framework/./viewsets.py) |
| 29 | + |
| 30 | +```python |
| 31 | +# viewsets.py |
| 32 | +from collections import OrderedDict |
| 33 | +from functools import update_wrapper |
| 34 | +from inspect import getmembers |
| 35 | + |
| 36 | +from django.urls import NoReverseMatch |
| 37 | +from django.utils.decorators import classonlymethod |
| 38 | +~~from django.views.decorators.csrf import csrf_exempt |
| 39 | + |
| 40 | +from rest_framework import generics, mixins, views |
| 41 | +from rest_framework.decorators import MethodMapper |
| 42 | +from rest_framework.reverse import reverse |
| 43 | + |
| 44 | + |
| 45 | +def _is_extra_action(attr): |
| 46 | + return hasattr(attr, 'mapping') and isinstance(attr.mapping, MethodMapper) |
| 47 | + |
| 48 | + |
| 49 | +class ViewSetMixin: |
| 50 | + |
| 51 | + @classonlymethod |
| 52 | + def as_view(cls, actions=None, **initkwargs): |
| 53 | + cls.name = None |
| 54 | + cls.description = None |
| 55 | + |
| 56 | + cls.suffix = None |
| 57 | + |
| 58 | + cls.detail = None |
| 59 | + |
| 60 | + cls.basename = None |
| 61 | + |
| 62 | + if not actions: |
| 63 | + |
| 64 | + |
| 65 | +## ... source file abbreviated to get to csrf_exempt examples ... |
| 66 | + |
| 67 | + |
| 68 | + def view(request, *args, **kwargs): |
| 69 | + self = cls(**initkwargs) |
| 70 | + |
| 71 | + if 'get' in actions and 'head' not in actions: |
| 72 | + actions['head'] = actions['get'] |
| 73 | + |
| 74 | + self.action_map = actions |
| 75 | + |
| 76 | + for method, action in actions.items(): |
| 77 | + handler = getattr(self, action) |
| 78 | + setattr(self, method, handler) |
| 79 | + |
| 80 | + self.request = request |
| 81 | + self.args = args |
| 82 | + self.kwargs = kwargs |
| 83 | + |
| 84 | + return self.dispatch(request, *args, **kwargs) |
| 85 | + |
| 86 | + update_wrapper(view, cls, updated=()) |
| 87 | + |
| 88 | + update_wrapper(view, cls.dispatch, assigned=()) |
| 89 | + |
| 90 | + view.cls = cls |
| 91 | + view.initkwargs = initkwargs |
| 92 | + view.actions = actions |
| 93 | +~~ return csrf_exempt(view) |
| 94 | + |
| 95 | + def initialize_request(self, request, *args, **kwargs): |
| 96 | + request = super().initialize_request(request, *args, **kwargs) |
| 97 | + method = request.method.lower() |
| 98 | + if method == 'options': |
| 99 | + self.action = 'metadata' |
| 100 | + else: |
| 101 | + self.action = self.action_map.get(method) |
| 102 | + return request |
| 103 | + |
| 104 | + def reverse_action(self, url_name, *args, **kwargs): |
| 105 | + url_name = '%s-%s' % (self.basename, url_name) |
| 106 | + namespace = None |
| 107 | + if self.request and self.request.resolver_match: |
| 108 | + namespace = self.request.resolver_match.namespace |
| 109 | + if namespace: |
| 110 | + url_name = namespace + ':' + url_name |
| 111 | + kwargs.setdefault('request', self.request) |
| 112 | + |
| 113 | + return reverse(url_name, *args, **kwargs) |
| 114 | + |
| 115 | + @classmethod |
| 116 | + def get_extra_actions(cls): |
| 117 | + return [method for _, method in getmembers(cls, _is_extra_action)] |
| 118 | + |
| 119 | + |
| 120 | +## ... source file continues with no further csrf_exempt examples... |
| 121 | + |
| 122 | +``` |
| 123 | + |
0 commit comments