Add Custom Fields to Django Flatpages
I was attempting to build a sort of self-building website, where one could add new pages with custom URLs right from the admin interface. This would allow non-tech users to easily expand their website. After asking a few questions, I got pointed to some nice Django functionality that basically already does this: the Django flatpages app.
I got it up and running pretty painlessly, and it works for simple pages. However, it only has three fields built-in: URL, title, and content. I need a little more control than that- meta description, for example. After bugging plenty of people on the #django IRC channel, I've cobbled together this solution:
models.py
- from django.contrib.flatpages.models import FlatPage
- class FlatPageExtra(models.Model):
- flatpage = models.ForeignKey(FlatPage,
- edit_inline=models.STACKED,
- max_num_in_admin=1,
- num_in_admin=1)
- description = models.CharField(max_length=255,
- core=True)
- headline = models.CharField(max_length=255,
- core=True)
- class Admin:
- pass
Here we are creating a new model (call it whatever you want). Make a field (also named anything, I chose flatpage) that is a Foreign key to the FlatPage model- don't forget to import! This allows you to associate your custom fields with their respective flatpages. I specified edit_inline=models.STACKED, max_num_in_admin=1, num_in_admin=1 because it pretty much only makes sense to edit your custom fields on the same page as the built-in fields, and there's only going to be one of each (at least in this case). Database enthusiasts would probably also specify unique=True, but I don't care either way. Add whatever other fields you want associated with your flatpages.
Run syncdb, fire up your admin page, and take a look! Note: I included TinyMCE for my content box, but that's better covered in the Django wiki.
In your template, you can grab these values like any other ForeignKey field: {{flatpage.flatpageextra_set.all.0.description}}. Note the .0. because it's actually a collection, even though we know there's always just one item.
- django
- April 20th, 2008 @ 5:55 p.m.
- Add Comment

Posted by buy ambien on July 4th, 2008 @ 1:55 a.m.
Posted by Ultram on July 4th, 2008 @ 12:44 a.m.
Posted by valium on July 3rd, 2008 @ 9:44 p.m.
Posted by buy-adipex on July 3rd, 2008 @ 9:14 p.m.
Posted by None on July 3rd, 2008 @ 7:11 p.m.
Posted by xanax on July 3rd, 2008 @ 7:06 p.m.
Posted by Fioricet on July 3rd, 2008 @ 4:32 p.m.
Posted by Meridia on July 3rd, 2008 @ 1:59 p.m.
Posted by None on July 3rd, 2008 @ 10:20 a.m.
Posted by None on July 3rd, 2008 @ 10:19 a.m.
Posted by zoloft on July 3rd, 2008 @ 7:48 a.m.
Posted by None on July 3rd, 2008 @ 7:38 a.m.
Posted by Fioricet on July 3rd, 2008 @ 5:22 a.m.
Posted by meridia on July 3rd, 2008 @ 3:02 a.m.
Posted by None on July 3rd, 2008 @ 1:05 a.m.
Posted by None on July 3rd, 2008 @ 1:05 a.m.
Posted by Valium on July 3rd, 2008 @ 12:34 a.m.
Posted by Valium on July 3rd, 2008 @ 12:34 a.m.
Posted by zoloft on July 2nd, 2008 @ 11:41 p.m.
Posted by Xanax on July 2nd, 2008 @ 10:08 p.m.
Posted by xanax on July 2nd, 2008 @ 10:07 p.m.
Posted by xanax on July 2nd, 2008 @ 10:06 p.m.
Posted by fioricet on July 2nd, 2008 @ 7:37 p.m.
Posted by Tramadol on July 2nd, 2008 @ 7:28 p.m.
Posted by None on July 2nd, 2008 @ 6:40 p.m.
Posted by Ultram on July 2nd, 2008 @ 4:43 p.m.
Posted by Meridia on July 2nd, 2008 @ 3:50 p.m.
Posted by Meridia on July 2nd, 2008 @ 3:50 p.m.
Posted by Levitra on July 2nd, 2008 @ 2:12 p.m.
Posted by Valium on July 2nd, 2008 @ 12:45 p.m.
Posted by ambien on July 2nd, 2008 @ 11:39 a.m.
Posted by xanax on July 2nd, 2008 @ 9:40 a.m.
Posted by Adipex on July 2nd, 2008 @ 9:03 a.m.
Posted by None on July 2nd, 2008 @ 8:16 a.m.
Posted by None on July 2nd, 2008 @ 8:15 a.m.
Posted by tramadol on July 2nd, 2008 @ 6:23 a.m.
Posted by Ultram on July 2nd, 2008 @ 3:23 a.m.
Posted by levitra on July 1st, 2008 @ 11:55 p.m.
Posted by levitra on July 1st, 2008 @ 11:54 p.m.
Posted by Levitra on July 1st, 2008 @ 11:53 p.m.
Posted by ambien on July 1st, 2008 @ 7:48 p.m.
Posted by adipex on July 1st, 2008 @ 4:16 p.m.
Posted by Adipex on July 1st, 2008 @ 4:15 p.m.
Posted by effexor on July 1st, 2008 @ 1:40 p.m.
Posted by effexor on July 1st, 2008 @ 1:40 p.m.
Posted by Prednisone on July 1st, 2008 @ 11:02 a.m.
Posted by None on July 1st, 2008 @ 10:45 a.m.
Posted by None on July 1st, 2008 @ 10:14 a.m.
Posted by None on July 1st, 2008 @ 9:25 a.m.
Posted by lisinopril on July 1st, 2008 @ 8:49 a.m.
Posted by Avandia on July 1st, 2008 @ 6:38 a.m.
Posted by cephalexin on July 1st, 2008 @ 4:18 a.m.
Posted by Cymbalta on July 1st, 2008 @ 2:08 a.m.
Posted by None on July 1st, 2008 @ 12:17 a.m.
Posted by lamictal on June 30th, 2008 @ 11:48 p.m.
Posted by lamictal on June 30th, 2008 @ 11:47 p.m.
Posted by topamax on June 30th, 2008 @ 10:44 p.m.
Posted by Lipitor on June 30th, 2008 @ 9:29 p.m.
Posted by lipitor on June 30th, 2008 @ 9:28 p.m.
Posted by Lexapro on June 30th, 2008 @ 6:52 p.m.
Posted by Zoloft on June 30th, 2008 @ 6:24 p.m.
Posted by Zoloft on June 30th, 2008 @ 6:21 p.m.
Posted by topamax on June 30th, 2008 @ 4:20 p.m.
Posted by fioricet on June 30th, 2008 @ 3 p.m.
Posted by Meridia on June 30th, 2008 @ 11:44 a.m.
Posted by None on June 30th, 2008 @ 10:37 a.m.
Posted by Zoloft on June 30th, 2008 @ 8:38 a.m.
Posted by zoloft on June 30th, 2008 @ 8:37 a.m.
Posted by valium on June 30th, 2008 @ 8:32 a.m.
Posted by Fioricet on June 30th, 2008 @ 6:31 a.m.
Posted by xanax on June 30th, 2008 @ 5:33 a.m.
Posted by Meridia on June 30th, 2008 @ 4:30 a.m.
Posted by Meridia on June 30th, 2008 @ 4:29 a.m.
Posted by meridia on June 30th, 2008 @ 4:29 a.m.
Posted by valium on June 30th, 2008 @ 2:21 a.m.
Posted by Tramadol on June 30th, 2008 @ 2:17 a.m.
Posted by Xanax on June 30th, 2008 @ 12:07 a.m.
Posted by Ultram on June 29th, 2008 @ 10:25 p.m.
Posted by tramadol on June 29th, 2008 @ 9:49 p.m.
Posted by Ultram on June 29th, 2008 @ 7:33 p.m.
Posted by None on June 29th, 2008 @ 6:30 p.m.
Posted by Levitra on June 29th, 2008 @ 5:45 p.m.
Posted by levitra on June 29th, 2008 @ 5:08 p.m.
Posted by ambien on June 29th, 2008 @ 2:42 p.m.
Posted by Ambien on June 29th, 2008 @ 2:41 p.m.
Posted by ambien on June 29th, 2008 @ 12:55 p.m.
Posted by adipex on June 29th, 2008 @ 12:21 p.m.
Posted by Adipex on June 29th, 2008 @ 12:17 p.m.
Posted by None on June 29th, 2008 @ 11:11 a.m.
Posted by effexor on June 29th, 2008 @ 8:07 a.m.
Posted by effexor on June 29th, 2008 @ 8:06 a.m.
Posted by None on June 29th, 2008 @ 3:24 a.m.
Posted by None on June 29th, 2008 @ 3:11 a.m.
Posted by Prednisone on June 29th, 2008 @ 2:39 a.m.
Posted by lisinopril on June 28th, 2008 @ 9:29 p.m.
Posted by avandia on June 28th, 2008 @ 3:35 p.m.
Posted by cephalexin on June 28th, 2008 @ 11:17 a.m.
Posted by None on June 28th, 2008 @ 10:58 a.m.
Posted by None on June 28th, 2008 @ 10:58 a.m.
Posted by cymbalta on June 28th, 2008 @ 7:40 a.m.
Posted by cymbalta on June 28th, 2008 @ 7:40 a.m.
Posted by cymbalta on June 28th, 2008 @ 7:39 a.m.
Posted by None on June 28th, 2008 @ 6:11 a.m.
Posted by Lamictal on June 28th, 2008 @ 4:11 a.m.
Posted by Lamictal on June 28th, 2008 @ 4:11 a.m.
Posted by None on June 28th, 2008 @ 12:11 a.m.
Posted by Lipitor on June 27th, 2008 @ 9:48 p.m.
Posted by Lipitor on June 27th, 2008 @ 9:47 p.m.
Posted by NFeWkLLl on June 27th, 2008 @ 8:45 p.m.
Posted by None on June 27th, 2008 @ 7:19 p.m.
Posted by Lexapro on June 27th, 2008 @ 5:08 p.m.
Posted by topamax on June 27th, 2008 @ 1:28 p.m.
Posted by None on June 27th, 2008 @ 2:18 a.m.
Posted by zoloft on June 26th, 2008 @ 7:11 p.m.
Posted by Zoloft on June 26th, 2008 @ 7:10 p.m.
Posted by Sexy girl on June 26th, 2008 @ 6:48 p.m.
Posted by None on June 26th, 2008 @ 5:34 p.m.
Posted by fioricet under online on June 26th, 2008 @ 3:59 p.m.
Posted by Fioricet on June 26th, 2008 @ 3:59 p.m.
Posted by Meridia on June 26th, 2008 @ 12:34 p.m.
Posted by zoloft on June 26th, 2008 @ 12:21 p.m.
Posted by None on June 26th, 2008 @ 10:15 a.m.
Posted by Fioricet on June 26th, 2008 @ 10:09 a.m.
Posted by Valium on June 26th, 2008 @ 8:59 a.m.