Using Django flatpages content in other (class-based) views

The flatpages application that ships with Django is both (intentionally) simple and useful. In previous projects I’ve found myself both extending the models (to add data like meta tags) and duplicating the whole application (when I wanted to add a whole bunch of extra details, multiple content sections, or a WYSIWYG editor).

In a recent project I had the need to editable (by administrators) content in other views, and I turned to flatpages again for my solution.

What I did was to create a class-based view mixin to find the flatpage for a given URL (defaulting to the one defined in request.path), and include the resulting object in the context (once the title and content had been marked safe of course):

from django.contrib.flatpages.models import FlatPage


class FlatPageMixin(object):
    """
    Retrieves the FlatPage object for the specified url, and includes it in the
    context.

    If no url is specified, request.path is used.
    """
    url = None

    def get_context_data(self, **kwargs):
        if not self.url:
            self.url = self.request.path

        context = super(FlatPageMixin, self).get_context_data(**kwargsG)
        try:
            flatpage = FlatPage.objects.get(url=self.url)
            flatpage.title = mark_safe(flatpage.title)
            flatpage.content = mark_safe(flatpage.content)
            context["flatpage"] = flatpage
        except FlatPage.DoesNotExist:
            context["flatpage"] = None

        return context

Then you just need to ensure a flatpage exists for the expected (or specified) URL, add the mixin to your class-based view(s) and use {{ flatpage.title }} and {{ flatpage.content }} in the template(s).

Back to blogging (again)

Back in 2009, I gave up on my blog and switched to Tumblr. Now I’m back.

There’s nothing wrong with Tumblr per se, but I ending up using it for consumption instead of creation (and it’s far too easy to just re-post pictures of cat or – in my case – motorcycles).

I’ve been meaning to get back to doing some writing for a while now, and instead of using the perfectly good services Tumblr makes available, I decided I would create myself a brand, new shiny WordPress blog (self-hosted, of course). Why? Why not.

Whoever mumbled “procrastination” under their breath while reading the last paragraph are kindly asked to keep their opinions to themselves.

Now, there’s a couple of housekeeping things that I should make people aware of (just in case they care):

  • My Tumblr blog isn’t going away, but it going to be reserved for the aforementioned pictures of cats (and motorcycles) – you can find it at http://tumblr.schwuk.com
  • Obviously, http://blog.schwuk.com now points to my WordPress blog
  • I used to serve everything through FeedBurner, but it’s just another service to maintain (and it looks like Google doesn’t anymore) so I’ve set it up to re-direct back to http://blog.schwuk.com/feed/ – if you subscribe then you may or may not need to re-subscribe, but I will leave this as an exercise for you, dear reader

Right, time to come up with some content…