View "functions" that are callables?

65 views
Skip to first unread message

Christophe Pettus

unread,
May 15, 2024, 3:40:03 PMMay 15
to Django users
Hi,

I'm surprised I don't know this, but: Can a view "function" in a urlconf be a callable that is not actually a function, such as a class with a __call__ method?

Abdulfarid Olakunle

unread,
May 15, 2024, 3:43:18 PMMay 15
to django...@googlegroups.com
Yes, in Django, a view function in a URLconf can indeed be a callable that is not necessarily a function. It can be a class-based view where the class has a `__call__` method, effectively making it callable. This is a common practice and allows for more flexibility and organization in your code.


On Wed, May 15, 2024 at 20:39 Christophe Pettus <x...@thebuild.com> wrote:
Hi,

I'm surprised I don't know this, but: Can a view "function" in a urlconf be a callable that is not actually a function, such as a class with a __call__ method?

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/F70DCB4E-1307-42C2-AC62-CA2DC98DCD5B%40thebuild.com.

Faisal Mahmood

unread,
May 15, 2024, 5:12:54 PMMay 15
to django...@googlegroups.com
Yes, in Django, a view function in a URLconf can indeed be a callable that is not necessarily a function. You can use classes with a `__call__` method as views, often referred to as class-based views (CBVs). 

Class-based views offer more flexibility and organization than function-based views in some cases. They allow you to group related functionality together more easily, provide better code reuse through inheritance, and offer built-in methods for common HTTP operations like GET, POST, PUT, etc.

Here's a basic example of a class-based view:

```python
from django.http import HttpResponse
from django.views import View

class MyView(View):
    def get(self, request, *args, **kwargs):
        return HttpResponse("This is a GET request")

    def post(self, request, *args, **kwargs):
        return HttpResponse("This is a POST request")
```

You can then include this class-based view in your URLconf like this:

```python
from django.urls import path
from .views import MyView

urlpatterns = [
    path('myview/', MyView.as_view(), name='my-view'),
]
```

In this example, `MyView` is a class-based view with `get()` and `post()` methods, which handle GET and POST requests, respectively. When included in the URLconf using `.as_view()`, Django will internally call the `__call__` method of the class to handle the request.

On Thu, May 16, 2024, 12:38 AM Christophe Pettus <x...@thebuild.com> wrote:
Hi,

I'm surprised I don't know this, but: Can a view "function" in a urlconf be a callable that is not actually a function, such as a class with a __call__ method?

Anthony Flury

unread,
May 16, 2024, 2:35:59 AMMay 16
to django...@googlegroups.com
I can't see why not - as far as Python is concerned a callable is a callable.

There is a deep in the weeds way for a caller to determine if a callable is a __call__ method on a class, but I really doubt Django does anything close to that - what would be the benefit.

On Wed, May 15, 2024 at 8:39 PM Christophe Pettus <x...@thebuild.com> wrote:
Hi,

I'm surprised I don't know this, but: Can a view "function" in a urlconf be a callable that is not actually a function, such as a class with a __call__ method?

Reply all
Reply to author
Forward
0 new messages