Tip: Extending Django flatpages

I did a Google search and since nothing came up, I’m writing this little tip on creating your own CMS by extending Django’s flatpages. What’s good about flatpages is that they’re included in Django and has some basic code to get you started. But clearly they’re not enough if you want other people to administer a site.. you’ll want to add extra fields and special help texts for the admin. But we still don’t want to rewrite those ~150 lines of code, and they can really help you get past all the boring stuff and into the action.

Simply do the following:

cp -R /usr/share/python-support/python-django/django/contrib/flatpages my_project/my_flatpages

views.py

1
from my_project.my_flatpages.models import FlatPage

middleware.py

1
from my_project.my_flatpages.views import flatpage

urls.py

3
4
5
urlpatterns = patterns('my_project.my_flatpages.views',
    (r'^(?P<url>.*)$', 'flatpage'),
)

my_project.my_flatpages.middleware.FlatpageFallbackMiddleware has to be added to your MIDDLEWARE_CLASSES and my_project.my_flatpages to your INSTALLED_APPS and you’ll need to run manage.py syncdb, possibly changing the table name in models.py, so it doesn’t conflict with the old flatpages table. That’s basically it. After that you can work on the templates as described in the other howto’s, but now you have your own model to extend.

3 Responses to “Tip: Extending Django flatpages”

  1. Simon Greenwood Says:

    That’s really useful, a great find. One thing to remember is if you’re making flatpages cohabit with other apps, put the flatpages url expression last in url.py so it handles anything that doesn’t match any other url expressions that you have. It’s obvious if you think about it but it might save you a couple of hours head scratching.

  2. Joshua Gourneau Says:

    Thanks, this was very helpful to me as well!

  3. DyennepeTup Says:

    Hello.

    I’m still looking for http://www.jlbuildingsupplies.com – Bay Window
    Could you help me?

Leave a Reply