Views
View mixins and base views provided by prefab.views.
LoginRequiredMixin
Applies @login_required to the view. On unauthenticated POST requests, falls back to the GET response instead of redirecting.
from prefab.views import LoginRequiredMixin, PrefabDetailView
class AccountView(LoginRequiredMixin, PrefabDetailView):
model = Profile
template_name = 'account/profile.html'BreadcrumbsMixin
Adds a breadcrumbs list to template context. Override get_breadcrumbs() to build the trail:
from prefab.views import BreadcrumbsMixin, PrefabDetailView
class ArticleDetailView(BreadcrumbsMixin, PrefabDetailView):
model = Article
def get_breadcrumbs(self):
return [
{'url': '/', 'name': 'Home'},
{'url': '/articles/', 'name': 'Articles'},
]{# template #}
{% include "snippets/breadcrumbs.html" %}TrackEventsMixin
Stores GA-style events in the session under track_events. The track_events context processor picks them up and passes them to the template on the next request.
from prefab.views import TrackEventsMixin, PrefabCreateView
class ArticleCreateView(TrackEventsMixin, PrefabCreateView):
model = Article
def form_valid(self, form):
response = super().form_valid(form)
self.track_event(category='articles', action='created', label=self.object.title)
return responsePrefabFormView
Extends ModelFormMixin. Automatically sets user_created and user_modified on form instances when those fields exist.
from prefab.views import PrefabFormView
class CommentFormView(PrefabFormView):
model = Comment
form_class = CommentForm
success_url = '/comments/'PrefabDetailView / PrefabListView / PrefabTemplateView
Standard Django views with meta_title injected into context.
from prefab.views import PrefabDetailView, PrefabListView
class ArticleDetailView(PrefabDetailView):
model = Article
template_name = 'articles/detail.html'
# {{ meta_title }} available in template, derived from article.__str__
class ArticleListView(PrefabListView):
model = Article
template_name = 'articles/list.html'PrefabCreateView / PrefabUpdateView
Extend CreateWithInlinesView / UpdateWithInlinesView (django-extra-views) with PrefabFormView behaviour. PrefabUpdateView fixes a django-extra-views issue where self.object is prematurely replaced during inline construction.
from prefab.views import PrefabCreateView, PrefabUpdateView
from prefab.inlines import PrefabInline
class ImageInline(PrefabInline):
model = Image
fields = ['file', 'caption']
class ArticleCreateView(PrefabCreateView):
model = Article
form_class = ArticleForm
inlines = [ImageInline]
success_url = '/articles/'
class ArticleUpdateView(PrefabUpdateView):
model = Article
form_class = ArticleForm
inlines = [ImageInline]JSONView / JSONListView
Views that render context as a JSON response.
from prefab.views import JSONView, JSONListView
class ArticleStatusView(JSONView):
def get_context_data(self, **kwargs):
return {'status': 'ok', 'count': Article.objects.count()}
class ArticleJSONListView(JSONListView):
model = Article
# returns queryset serialized as JSON