New `pre_handler()` method on Class-Based Views

102 views
Skip to first unread message

Pablo Recio

unread,
Apr 3, 2016, 3:07:08 PM4/3/16
to Django developers (Contributions to Django itself)
Hey there,

Wanted to share an idea I had during some discussions in DjangoCon EU. When using CBV, sometimes we have to replicate behaviour between different handlers. For instance setting up certain objects, extra user checks... Normally the way to achieve this is to override the `dispatch()`, but it's a bit error-prone and doesn't feel too clean sometimes.

So I wrote a little piece of code that adds a `pre_handler()` method that gets called before the handler method, whatever that is. Here is the code: https://github.com/pablorecio/django/tree/pre-handler-view-method, if it's something that would get merged I'll happily submit a PR.

Thanks!
Pablo Recio


Ryan Hiebert

unread,
Apr 3, 2016, 3:35:26 PM4/3/16
to django-d...@googlegroups.com
I have taken to using a prepare method called from dispatch which takes the request  args, and I use it primarily to resolve ids in the url to db objects. This sounds like it fills a similar purpose.

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/df0c7703-b496-4bfa-ad31-68b35be482fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Graham

unread,
Apr 5, 2016, 9:20:23 AM4/5/16
to Django developers (Contributions to Django itself)
See the discussion on the pull request: https://github.com/django/django/pull/6416

Aymeric says, "The GCBV API is already widely criticized for being too complicated. I think that adding one method to save one line of code is a bad trade-off. I'm -1."

Diederik van der Boor

unread,
Apr 6, 2016, 7:29:33 AM4/6/16
to django-d...@googlegroups.com
Too bad the issue was already closed.

I’d expect having a View.setup() method would simplify things for both simple use case and complex projects.

Best,
Diederik

Reply all
Reply to author
Forward
0 new messages