python
So what are class-based generic views? A generic view is one which provides generic functionality. Implementing these types of views with classes allows the developer to customise this behaviour.
A simple example:
from pyramid_views import views
# An example TemplateView which simply renders the home template
class HomeView(views.TemplateView):
template_name = 'templates/home.pt'
And to add a route:
config.add_route('home', '/', HomeView.as_view())
The motivation here is to allow for flexible views while removing the need for boiler-plate code.
The functionality here is a direct port of Django's class-based views, so those docs are the recommended reference point.
However, there are a few differences to note:
Any reference to the term
queryset
has changed toquery
. For example:get_queryset()
is nowget_query()
- The
queryset
attribute is now thequery
attribute
- The
__all__
value for thefields
attribute is unsupported. Omit thefields
attribute entirely for the same behaviour.
Additional functionality includes:
- The
MacroMixin
class, for passing macros to chameleon templates via themacro_names
attribute. - The
DbSessionMixin
class makes the database session available viaself.db_session
- The
UpdateView
class supports thepartial_updates
option. WhenTrue
, this will only update fields present in the request - Form-based can take the
endpoint
parameter. WhenTrue
, the view will behave like an API endpoint rather than a user-facing page (errors encoded as JSON, 200 response codes whensuccess_uri
not present).