[Django]-Extend flatpages for a specific application


Of course. Just write your own flatpage-model, for example:

from django.contrib.flatpages.models import FlatPage

class MyFlatPage(FlatPage):
    prefix = models.CharField(max_length=100, editable=False)
    url = models.CharField(_('URL'), max_length=100, db_index=True, editable=False)

Then add a proper MyFlatPageAdmin to your admin.py file (if you want to, you can import the flatpageadmin from django.contrib.flatpages.admin and inherit from it). After all, you’re using the flatpage-model, but overwrite the urls-field. Now add a signal, which concats the prefix and a automatically generated url suffix to the url (like the object id). You can now add the custom flatpage like:

flatpage = MyFlatPage(prefix='/portfolio/my-super-project/')

Everything clear now?

edit 1

As you can read in the documentation, every flatpage should be a projectflatpage and vice versa.


re the ‘Edit 2’ part of your question… since your custom flatpage model inherits from FlatPage there is an automatic one-to-one field relation created between them by Django.

So in your template, if you know that the flatpage object passed in is one of your customised ones you can access your extra attributes by:

{{ flatpage.projectflatpage.project }}

Leave a comment