Base views#
This page lists the view classes your portal subclasses: the seven base views in fairdm.views and the mixin for views behind a record. It gives each class’s parents, attributes, hooks and context. It does not explain why the classes are layered this way, which is in How FairDM views are layered, and it does not cover the home page view, which is in Home page.
All seven classes are importable from fairdm.views:
from fairdm.views import (
FairDMCreateView,
FairDMDeleteView,
FairDMDetailView,
FairDMListView,
FairDMTableView,
FairDMTemplateView,
FairDMUpdateView,
)
Each class puts django-mvp’s view under django-meta’s MetadataMixin. django-mvp draws the page inside the application shell. MetadataMixin adds a meta object to the context.
Which view to subclass#
To build |
Subclass |
Based on |
|---|---|---|
A page with a template and no object |
|
|
A list of objects as cards |
|
|
A list of objects as a table |
|
|
A page for one object |
|
|
A form that creates an object |
|
|
A form that edits an object |
|
|
A confirmation page that deletes an object |
|
|
FairDMTemplateView#
A page that renders a template.
Based on:
MetadataMixin,MVPTemplateView.Default
template_name:"page_view.html", django-mvp’s page template.Context:
meta,page.
from fairdm.views import FairDMTemplateView
class AboutView(FairDMTemplateView):
template_name = "myapp/about.html"
page_title = "About us"
title = "About us"
description = "Learn about our research data portal."
FairDMListView#
A paginated list drawn as cards or custom items. The search, the sort and the filters all come from one filterset.
Based on:
MetadataMixin,MVPListView.Context:
meta,page,grid_config,list_item_template,empty_state,filter,object_list.
Attribute |
Type |
Default |
What it does |
|---|---|---|---|
|
model class |
None |
The model to list |
|
|
|
Objects per page |
|
dict |
|
The responsive grid, passed to the template as |
|
|
|
The template drawn for each item. |
|
|
|
The filterset that holds the search ( |
|
list of |
|
The names of the filterset fields drawn in the toolbar. The others sit in the filter panel |
|
|
“There’s nothing here yet” |
The heading shown when the list is empty. |
|
|
A line pointing at the create button |
The text under that heading. It is left out for a user who cannot create |
Hooks: get_queryset(), get_grid_config(), get_list_item_template(), get_empty_state_heading() and get_empty_state_message().
A view that declares search_fields or order_by, or that also inherits django-filter’s FilterView, raises ImproperlyConfigured when its module is imported. Put the search and the sort on the filterset. See How to add search and sort to a list view.
import django_filters
from mvp.filters import SearchFilter, SortFilter
from fairdm.views import FairDMListView
from .models import Project
class ProjectFilter(django_filters.FilterSet):
q = SearchFilter(fields=["name", "uuid"])
o = SortFilter(
options=[
("name", "Name (A-Z)", ["name", "pk"]),
("-added", "Newest first", ["-added", "-pk"]),
]
)
class Meta:
model = Project
fields = ["status"]
class ProjectListView(FairDMListView):
model = Project
filterset_class = ProjectFilter
toolbar_filters = ["q", "status"]
grid = {"cols": 1, "sm": 2, "lg": 3}
FairDMTableView#
A paginated list drawn as a table by django-tables2. The template must render {{ table }}. Nothing renders it for you.
Based on:
MetadataMixin,MVPTableView.Context:
meta,page,table,filter.
Attribute |
Type |
Default |
What it does |
|---|---|---|---|
|
model class |
None |
The model to list |
|
|
None |
The columns. Required |
|
|
None |
The template that renders |
|
|
|
The filterset that holds the search and the filters. A |
|
list of |
|
The names of the filterset fields drawn in the toolbar |
|
|
|
Rows per page. It is the only page-size control. It takes precedence over |
A view that declares order_by raises ImproperlyConfigured. Put the ordering on the table class.
import django_tables2 as tables
from fairdm.views import FairDMTableView
from .filters import StationFilter
from .models import Station
class StationTable(tables.Table):
class Meta:
model = Station
fields = ["name", "code", "added"]
class StationTableView(FairDMTableView):
model = Station
table_class = StationTable
filterset_class = StationFilter
template_name = "stations/station_table.html"
FairDMDetailView#
A page for one object. It adds nothing to MVPDetailView apart from the metadata.
Based on:
MetadataMixin,MVPDetailView.Context:
meta,page,object.Attributes:
model, plus Django’sslug_fieldandpk_url_kwarg.Default page title: the object’s string form.
from django.shortcuts import get_object_or_404
from fairdm.views import FairDMDetailView
from .models import Station
class StationDetailView(FairDMDetailView):
model = Station
template_name = "stations/station_detail.html"
def get_object(self, queryset=None):
return get_object_or_404(Station, code=self.kwargs["code"])
The pages of a project, dataset, sample or measurement are not subclasses of this class. Each is a plugin. See Plugins.
FairDMCreateView#
A form that creates an object. It adds no attributes of its own.
Based on:
MetadataMixin,MVPCreateView.Context:
meta,page,form.Attributes:
model,fieldsorform_class,success_url.Default page title: “Create” and the model’s name.
from fairdm.views import FairDMCreateView
from .models import Station
class StationCreateView(FairDMCreateView):
model = Station
fields = ["name", "code"]
success_url = "list"
success_url is a literal path or one of django-mvp’s shorthands for the model’s other pages, such as "list" and "detail". A view with no success_url redirects to the object’s get_absolute_url(), and raises ImproperlyConfigured when there is none. A ?next= address that stays on the site is used first.
FairDMUpdateView#
A form that edits an object.
Based on:
MetadataMixin,MVPUpdateView.Context:
meta,page,form,object, anddelete_urlwhen a deletion page is registered.Attributes:
model,fieldsorform_class,success_url, with the same rules asFairDMCreateView.Default page title: “Update” and the model’s name.
from fairdm.views import FairDMUpdateView
from .models import Station
class StationUpdateView(FairDMUpdateView):
model = Station
fields = ["name", "code"]
success_url = "detail"
FairDMDeleteView#
A confirmation page that deletes an object.
Based on:
MetadataMixin,MVPDeleteView.Context:
meta,page,object, and the deletion preview and protection state.
Attribute |
Type |
Default |
What it does |
|---|---|---|---|
|
model class |
None |
The model to delete |
|
|
None |
Where to go after deleting. A literal path or a shorthand such as |
|
|
|
Lists the records that deleting also deletes |
|
|
|
Makes the user type the object’s name before Delete works |
|
|
“Type the name to confirm” |
The label of that input |
|
|
|
How many related objects are listed in each group |
|
dict |
|
Attributes of the alert that holds the related-objects summary |
Hooks: get_confirmation_value(), get_back_url(), get_back_url_fallback(), get_success_url().
FairDM changes two behaviours of MVPDeleteView:
A record that something else relies on cannot be deleted, and the page says so instead of offering the confirmation. It lists what is in the way and draws no submit control. This covers relations declared
on_delete=PROTECTandon_delete=RESTRICT.get_back_url()uses the?backaddress only when it stays on the site. Otherwise it returnsget_back_url_fallback(), which is the model’s list page, or an empty string when there is none.
from fairdm.views import FairDMDeleteView
from .models import Station
class StationDeleteView(FairDMDeleteView):
model = Station
success_url = "list"