A little Django flatpage trick

For each general area of my humble little website, I have a base template that takes care of a links bar and breadcrumbs and things. All the individual pages in that area extend that base.html, which in turn extends parent base templates. Nothing unusual there, and all well and good.

Except for flatpages. Flatpages are great, but out of the box they’re a tiny bit rigid. I want a flatpage in a particular area to extend the right base.html, but that’s not quite what the flatpage template setting provides—that’s a whole template, and I just want a base to extend. One could of course have a different flatpage template for each base.html, but that’s gross, and not very DRY.

Maybe there is some simple obvious way to do this built in to Django, but I couldn’t find it. My solution is to extract the appropriate base template from the flatpage itself. I put it in a django comment, which will get stripped out of the content before rendering. So the line

{# Base utilities/base.html #}

goes in the body of the flatpage, and gets extracted with a filter. [It might be better to infer it from the page’s url, if one’s directory structure and url structure always match.] The only problem is that I need the filter loaded before the extends tag, and the extends tag needs to come before anything else. It used to be possible to load before extending, but that was an evil (if useful) loophole, now closed.

Like all problems in computer science, this can be solved with another level of indirection. flatpages/default.html loads the filter and extracts the base template name, and then includes another template to do the actual rendering.

Here’s the code, simple and completely non-robust though it is. In a tempatetags/flatpage_utils.py or whatever you want to call it:

@register.filter
@stringfilter
def stripdjangocomments(text):
    """
    Strip django comments from the text.
    """
    s = re.sub(r'{#.*?#}', '', text)
    return s

@register.filter
@stringfilter
def getbase(text, default = "base.html"):
    """
    Look for a string of the form {# Base foo #} and return foo
    """
    m = re.search(r'{#\s*Base\s*(\S*?)\s*#}', text)
    if m and m.groups()[0]:
        return m.groups()[0]
    else:
        return default

In templates/flatpages/default.html

{% load flatpage_utils %}

{% with flatpage.content|getbase as pagebase %}
{% include "flatpages/flatpagebody.html" %}
{% endwith %}

And in templates/flatpages/flatpagebody.html

{% extends pagebase %}
{% load whatever_else %}

{% block title %}
{{ flatpage.title }}
{% endblock %}

{# maybe other stuff #}

{% block content %}
{# add more filters if you like #}
{{ flatpage.content|stripdjangocomments }}
{% endblock %}

And that’s it.

Advertisements

Tags: , ,

4 Responses to “A little Django flatpage trick”

  1. wiz Says:

    Why the hell put hacks in page when you can write it into a field dedicated especially for it?

  2. mrlauer Says:

    Because that’s not exactly what the field is dedicated especially for. If I’m missing something, feel free to explain in slightly more detail, and I’ll cheerfully admit my denseness.

    You could define all your base templates to use flatpages if given them, but did not seem any better.

  3. Joshua Jonah Says:

    My flatpages/default.html:

    {% extends base.html %}

    {% block title %}{{ flatpage.title }}{% endblock %}

    {% block content %}{{ flatpage.content }}{% endblock %}

    • Amin Aman Says:

      how do you extend the “base.html” in flatpages without following the tutorial of mrlauer ( it s a great tutorial by the way 🙂 ) ? thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: