Main entry point for a request-response process.
(cls, **initkwargs)
| 80 | |
| 81 | @classonlymethod |
| 82 | def as_view(cls, **initkwargs): |
| 83 | """Main entry point for a request-response process.""" |
| 84 | for key in initkwargs: |
| 85 | if key in cls.http_method_names: |
| 86 | raise TypeError( |
| 87 | "The method name %s is not accepted as a keyword argument " |
| 88 | "to %s()." % (key, cls.__name__) |
| 89 | ) |
| 90 | if not hasattr(cls, key): |
| 91 | raise TypeError( |
| 92 | "%s() received an invalid keyword %r. as_view " |
| 93 | "only accepts arguments that are already " |
| 94 | "attributes of the class." % (cls.__name__, key) |
| 95 | ) |
| 96 | |
| 97 | def view(request, *args, **kwargs): |
| 98 | self = cls(**initkwargs) |
| 99 | self.setup(request, *args, **kwargs) |
| 100 | if not hasattr(self, "request"): |
| 101 | raise AttributeError( |
| 102 | "%s instance has no 'request' attribute. Did you override " |
| 103 | "setup() and forget to call super()?" % cls.__name__ |
| 104 | ) |
| 105 | return self.dispatch(request, *args, **kwargs) |
| 106 | |
| 107 | view.view_class = cls |
| 108 | view.view_initkwargs = initkwargs |
| 109 | |
| 110 | # __name__ and __qualname__ are intentionally left unchanged as |
| 111 | # view_class should be used to robustly determine the name of the view |
| 112 | # instead. |
| 113 | view.__doc__ = cls.__doc__ |
| 114 | view.__module__ = cls.__module__ |
| 115 | view.__annotations__ = cls.dispatch.__annotations__ |
| 116 | # Copy possible attributes set by decorators, e.g. @csrf_exempt, from |
| 117 | # the dispatch method. |
| 118 | view.__dict__.update(cls.dispatch.__dict__) |
| 119 | |
| 120 | # Mark the callback if the view class is async. |
| 121 | if cls.view_is_async: |
| 122 | markcoroutinefunction(view) |
| 123 | |
| 124 | return view |
| 125 | |
| 126 | def setup(self, request, *args, **kwargs): |
| 127 | """Initialize attributes shared by all view methods.""" |
no test coverage detected