Permalink
Fetching contributors…
Cannot retrieve contributors at this time
517 lines (316 sloc) 18.5 KB

Buildable views

Django's class-based views are used to render HTML pages to flat files. Putting all the pieces together is a little tricky at first, particularly if you haven't studied the Django source code or lack experience working with Python classes in general. But if you figure it out, we think it's worth the trouble.

BuildableTemplateView

BuildableListView

Render and builds a page about a list of objects. Extended from Django's generic ListView. The base class has a number of options not documented here you should consult.

Example myapp/views.py

from myapp.models import MyModel
from bakery.views import BuildableListView


class ExampleListView(BuildableListView):
    model = MyModel
    template_name = 'mymodel_list.html'


class DifferentExampleListView(BuildableListView):
    build_path = 'mymodel/index.html'
    queryset = MyModel.objects.filter(is_published=True)
    template_name = 'mymodel_list.html'

BuildableDetailView

Render and build a "detail" page about an object or a series of pages about a list of objects. Extended from Django's generic DetailView. The base class has a number of options not documented here you should consult.

Example myapp/models.py

from django.db im­port mod­els
from bakery.mod­els im­port Build­ableMod­el


class My­Mod­el(Build­ableMod­el):
    de­tail_views = ('myapp.views.ExampleDetailView',)
    title = mod­els.Char­Field(max_length=100)
    slug = models.SlugField(max_length=100)

    def get_absolute_url(self):
        """
        If you are going to publish a detail view for each object,
        one easy way to set the path where it will be built is to
        configure Django's standard get_absolute_url method.
        """
        return '/%s/' % self.slug

Example myapp/views.py

from myapp.models import MyModel
from bakery.views import BuildableDetailView


class ExampleDetailView(BuildableListView):
    queryset = MyModel.objects.filter(is_published=True)
    template_name = 'mymodel_detail.html'

BuildableArchiveIndexView

Renders and builds a top-level index page showing the “latest” objects, by date. Extended from Django's generic ArchiveIndexView. The base class has a number of options not documented here you should consult.

Example myapp/views.py

from myapp.models import MyModel
from bakery.views import BuildableArchiveIndexView


class ExampleArchiveIndexView(BuildableArchiveIndexView):
    model = MyModel
    date_field = "pub_date"


class DifferentExampleArchiveIndexView(BuildableArchiveIndexView):
    build_path = 'my-archive-directory/index.html'
    queryset = MyModel.objects.filter(is_published=True)
    date_field = "pub_date"
    template_name = 'mymodel_list.html'

BuildableYearArchiveView

Renders and builds a yearly archive showing all available months (and, if you'd like, objects) in a given year. Extended from Django's generic YearArchiveView. The base class has a number of options not documented here you should consult.

Example myapp/views.py

from myapp.models import MyModel
from bakery.views import BuildableYearArchiveView


class ExampleArchiveYearView(BuildableYearArchiveView):
    model = MyModel
    date_field = "pub_date"

BuildableMonthArchiveView

Renders and builds a monthly archive showing all objects in a given month. Extended from Django's generic MonthArchiveView. The base class has a number of options not documented here you should consult.

Example myapp/views.py

from myapp.models import MyModel
from bakery.views import BuildableMonthArchiveView


class ExampleMonthArchiveView(BuildableMonthArchiveView):
    model = MyModel
    date_field = "pub_date"

BuildableDayArchiveView

Renders and builds a day archive showing all objects in a given day. Extended from Django's generic DayArchiveView. The base class has a number of options not documented here you should consult.

Example myapp/views.py

from myapp.models import MyModel
from bakery.views import BuildableDayArchiveView


class ExampleDayArchiveView(BuildableDayArchiveView):
    model = MyModel
    date_field = "pub_date"

Buildable404View

Renders and builds a simple 404 error page template as a flat file. Extended from the BuildableTemplateView above. The base class has a number of options not documented here you should consult.

All it does

from bakery.views import BuildableTemplateView


class Buildable404View(BuildableTemplateView):
    build_path = '404.html'
    template_name = '404.html'

BuildableRedirectView

Render and build a redirect. Extended from Django's generic RedirectView. The base class has a number of options not documented here you should consult.

Example myapp/views.py

from bakery.views import BuildableRedirectView


class ExampleRedirectView(BuildableRedirectView):
    build_path = "mymodel/oldurl.html"
    url = '/mymodel/'