Login

Tag "views class-based-views classes metaclass"

Snippet List

Convenient class based views

I needed to use class based views, but I wanted to be able to use the full name of the class in my URLconf without always having to instantiate the view class before using it. What helped me was a surprisingly simple metaclass. I can now both instantiate view classes and use the instances as view functions, OR I can simply point my URLconf to my class and have the metaclass instantiate (and call) the view class for me. This works by checking the first argument to `__call__` – if it's a `HttpRequest`, it must be an actual HTTP request because it would be nonsense to attept to instantiate a view class with an `HttpRequest` instance. The `View` base class contains a overridable before method that can be used to add a common procedure to handlers of different HTTP requests. The `before` method can modify the args and kwargs of the request handlers to be able to replace, say, `model_id` with `model`.

  • views class-based-views classes metaclass
Read More

1 snippet posted so far.