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?
>
> --
> 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+unsubscr...@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
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAP3eejziJxgJR6UQZ4%2B7pUH_12rwez7yJYEp%3DwjD2%3D%3DXvhdGzw%40mail.gmail.com.

Reply via email to