What was the rationale behind ClassBasedView.as_view()

27 views
Skip to first unread message

Frederik Creemers

unread,
Mar 13, 2017, 2:36:46 PM3/13/17
to Django users
Django views are just functions that take a request, and possibly some other args, and return a response. To get such a view from a class based view, you can  `as_view()` on it. Why is that? Wouldn't it make the code neater to just implement `__call__` on the views? What was the reing behind having `as_view`?

Daniel Roseman

unread,
Mar 13, 2017, 5:48:48 PM3/13/17
to Django users
On Monday, 13 March 2017 18:36:46 UTC, Frederik Creemers wrote:
Django views are just functions that take a request, and possibly some other args, and return a response. To get such a view from a class based view, you can  `as_view()` on it. Why is that? Wouldn't it make the code neater to just implement `__call__` on the views? What was the reing behind having `as_view`?

A search in django-developers would probably reveal the extremely long-running thread that led to that specific implementation.

But, briefly, allowing the CBVs to be instantiated in the URLs would render them non-thread safe. Any attribute set on the object would persist across all future requests, leading to information leakage and other very bad behaviour. The `as_view()` construct is a (mostly successful) effort to make it almost impossible to break the thread safety of the view objects.
--
DR.

Frederik Creemers

unread,
Mar 13, 2017, 10:09:22 PM3/13/17
to Django users
That makes heaps of sense, thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/ih8_zwLQVgc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1b6323e5-ca93-4ccf-91d5-da7d5a72f6b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Russell Keith-Magee

unread,
Mar 13, 2017, 11:57:39 PM3/13/17
to Django Users
There’s also a summary of the various options that were considered:


Yours,
Russ Magee %-)

On Tue, Mar 14, 2017 at 10:08 AM, Frederik Creemers <frederik...@gmail.com> wrote:
That makes heaps of sense, thanks!

On Mon, Mar 13, 2017 at 10:49 PM Daniel Roseman <dan...@roseman.org.uk> wrote:
On Monday, 13 March 2017 18:36:46 UTC, Frederik Creemers wrote:
Django views are just functions that take a request, and possibly some other args, and return a response. To get such a view from a class based view, you can  `as_view()` on it. Why is that? Wouldn't it make the code neater to just implement `__call__` on the views? What was the reing behind having `as_view`?

A search in django-developers would probably reveal the extremely long-running thread that led to that specific implementation.

But, briefly, allowing the CBVs to be instantiated in the URLs would render them non-thread safe. Any attribute set on the object would persist across all future requests, leading to information leakage and other very bad behaviour. The `as_view()` construct is a (mostly successful) effort to make it almost impossible to break the thread safety of the view objects.
--
DR.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/ih8_zwLQVgc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages