Views#
- class abstract.views.CreateAPIView(**kwargs)[source]#
A view for creating instances of a model using Django REST Framework’s CreateAPIView.
- Inherits:
generics.CreateAPIView
- Usage:
Define your view class inheriting from this class and specify the serializer_class attribute with the appropriate serializer for creating instances.
- Example:
- class MyCreateAPIView(CreateAPIView):
serializer_class = MyModelSerializer
- class abstract.views.CreateView(**kwargs)[source]#
A view for creating new instances of a model, with login required, inheriting from Django’s generic CreateView.
- Inherits:
edit.CreateView
- Attributes:
template_name (str): Path to the template for rendering the create view.
- Methods:
get_form_kwargs(): Retrieve keyword arguments for form instantiation.
- Usage:
Define your view class inheriting from this class and specify the model and form_class attributes.
- Example:
- class MyCreateView(CreateView):
model = MyModel form_class = MyForm
- Notes:
The template for rendering the create view is determined by the ‘template_name’ attribute.
The form for creating instances is automatically generated based on the ‘form_class’ attribute.
Additional keyword arguments for form instantiation can be retrieved by overriding the ‘get_form_kwargs’ method.
- class abstract.views.DeleteView(**kwargs)[source]#
A view for deleting an instance of a model, with login required, inheriting from Django’s generic DeleteView.
- Inherits:
edit.DeleteView
- Attributes:
template_name (str): Path to the template for rendering the delete view.
- Usage:
Define your view class inheriting from this class and specify the model attribute.
- Example:
- class MyDeleteView(DeleteView):
model = MyModel
- Notes:
The template for rendering the delete view is determined by the ‘template_name’ attribute.
Confirmation of deletion is handled by the associated template.
- class abstract.views.DetailChildView(**kwargs)[source]#
A view rendering details of child objects related to a main object, with login required, inheriting from DetailView.
- Inherits:
DetailView
- Attributes:
template_name (str): Path to the template for rendering the detail view. field (str): The field name specifying the relationship to the child objects. child_header (str): Header for the child view, if applicable. filter_by_user (bool): Flag indicating whether to filter child objects based on the user.
- Methods:
get_objects(context): Retrieve child objects related to the main object. get_context_data(**kwargs): Retrieve context data including child objects.
- Usage:
Define your view class inheriting from this class and specify the model attribute with the model whose details are to be displayed, along with other necessary attributes.
- Example:
- class MyDetailChildView(DetailChildView):
model = MyModel field = ‘related_objects’
- Notes:
The template for rendering the detail view is determined by the ‘template_name’ attribute.
Child objects are fetched automatically based on the relationship specified in ‘field’.
Child objects can be filtered based on the user if ‘filter_by_user’ is set to True.
- class abstract.views.DetailView(**kwargs)[source]#
A view rendering the details of a single object, with login required, inheriting from Django’s generic DetailView.
- Inherits:
generic.DetailView
- Attributes:
template_name (str): Path to the template for rendering the detail view.
- Usage:
Define your view class inheriting from this class and specify the model attribute with the model whose details are to be displayed.
- Example:
- class MyDetailView(DetailView):
model = MyModel
- Notes:
The template for rendering the detail view is determined by the ‘template_name’ attribute.
The object to display details for is fetched automatically based on the URL parameters.
- class abstract.views.FormView(**kwargs)[source]#
A view rendering a form, with login required, inheriting from Django’s generic FormView.
- Inherits:
generic.FormView
- Usage:
Define your view class inheriting from this class and specify the form_class attribute with the appropriate form class.
- Example:
- class MyFormView(FormView):
form_class = MyForm
- Note:
Make sure to define the template_name attribute to specify the template for rendering the form. By default, it looks for a template with the name <app>/<model>_form.html.
- class abstract.views.ListView(**kwargs)[source]#
A view rendering a list of objects, with login required, inheriting from Django’s generic ListView.
- Inherits:
generic.ListView
- Attributes:
paginate_by (int): Number of objects to display per page. template_name (str): Path to the template for rendering the list view. child_header (str): Header for the child view, if applicable.
- Methods:
get_queryset(): Returns the queryset of objects to display. get_context_data(**kwargs): Returns the context data to pass to the template.
- Usage:
Define your view class inheriting from this class and specify the model attribute with the model to list.
- Example:
- class MyListView(ListView):
model = MyModel
- Notes:
The template for rendering the list view is determined by the ‘template_name’ attribute.
Pagination is enabled by default and can be adjusted using the ‘paginate_by’ attribute.
Additional context data can be provided to the template by overriding the ‘get_context_data’ method.
- class abstract.views.RedirectView(**kwargs)[source]#
A view for redirecting to a specified URL, inheriting from Django’s generic RedirectView.
- Inherits:
base.RedirectView
- Attributes:
permanent (bool): Flag indicating whether the redirection is permanent. url (str): URL to redirect to.
- Usage:
Define your view class inheriting from this class and specify the ‘url’ attribute.
- Example:
- class MyRedirectView(RedirectView):
url = ‘/my-redirect-url/’
- Notes:
The ‘permanent’ attribute determines whether the redirection is permanent (HTTP status code 301) or temporary (HTTP status code 302).
The ‘url’ attribute specifies the destination URL to redirect to.
- class abstract.views.TemplateView(**kwargs)[source]#
A view rendering a template, with login required, inheriting from Django’s generic TemplateView.
- Inherits:
generic.TemplateView
- Usage:
Define your view class inheriting from this class and specify the template_name attribute with the path to the template to render.
- Example:
- class MyTemplateView(TemplateView):
template_name = “my_template.html”
- class abstract.views.UpdateView(**kwargs)[source]#
A view for updating an instance of a model, with login required, inheriting from Django’s generic UpdateView.
- Inherits:
edit.UpdateView
- Attributes:
template_name (str): Path to the template for rendering the update view.
- Methods:
get_form_kwargs(): Retrieve keyword arguments for form instantiation.
- Usage:
Define your view class inheriting from this class and specify the model attribute.
- Example:
- class MyUpdateView(UpdateView):
model = MyModel
- Notes:
The template for rendering the update view is determined by the ‘template_name’ attribute.
The form for updating instances is automatically generated based on the model.
Additional keyword arguments for form instantiation can be retrieved by overriding the ‘get_form_kwargs’ method.