Generic Views

synopsis:

Generic view class in django-boost

Extended Views (deprecated)

Note

The base View class, its after_view_process hook, and the generic-view aliases (TemplateView, FormView, CreateView, ListView, DetailView, UpdateView, DeleteView) are deprecated and will be removed in django-boost 4.0. They re-implement as_view and do not support async views. Use Django’s own django.views.generic classes, and override dispatch() (or use middleware) where you previously used after_view_process:

from django.views.generic import TemplateView

class YourView(TemplateView):

    def dispatch(self, request, *args, **kwargs):
        response = super().dispatch(request, *args, **kwargs)
        ## post-process the response here, e.g. add a header
        return response

JsonView

class django_boost.views.generic.JsonView(**kwargs)

JsonResponse view.

A generic view class that inherits JsonResponseMixin and JsonRequestMixin.

from django_boost.views.generic import JsonView

class SameAPIView(JsonView):

    def get_context_data(self, **kwargs):
        return self.json

In the above example, we just return the sent Json string as it is.

StringView

class django_boost.views.simple.StringView(**kwargs)

Create simple string response from content.

Pass keyword arguments from the URLconf to the context.

from django_boost.views.simple import StringView

class MyStringView(StringView):
    content = 'Hello World!'

class MyDynamicStringView(StringView):

    def get_content(self, **kwargs):
        return 'Hello World!'

ModelCRUDViews

Provides easy creation of CRUDViews linked to model.

from django_boost.views.generic import ModelCRUDViews

class CustomerViews(ModelCRUDViews):
    model = Customer
from django.urls import path, include
from . import views

urlpatterns = [
    path('views/', include(views.CustomerViews().urls)),
]

In the template you can use as follows.

{% url 'customer:list' %}
{% url 'customer:create' %}
{% url 'customer:detail' %}
{% url 'customer:update' %}
{% url 'customer:delete' %}

The name of the URL is defined under the namespace of the lower-cased model class name.

Case of Namespaced

from django.urls import path, include
from . import views

app_name = "myapp"
urlpatterns = [
    path('views/', include(views.CustomerViews(app_name="myapp:customer").urls)),
]

In the template you can use as follows.

{% url 'myapp:customer:list' %}
{% url 'myapp:customer:create' %}
{% url 'myapp:customer:detail' %}
{% url 'myapp:customer:update' %}
{% url 'myapp:customer:delete' %}