Specific templates for flatpages in Django

3.4k Views Asked by At

Is it possible to link specific templates for different flatpages in Django?

For example:

/about/    ->   templates/flatpages/about.html
/contact/  ->   templates/flatpages/contact.html

This is what I have but all these pages point to the default.html template

url(r'^(?P<url>about/)$', 'django.contrib.flatpages.views.flatpage'),
url(r'^(?P<url>contact/)$', 'django.contrib.flatpages.views.flatpage'),
url(r'^(?P<url>feedback/)$', 'django.contrib.flatpages.views.flatpage'),
2

There are 2 best solutions below

0
On BEST ANSWER

I don't know what version of Django you have, but in 1.1.1 there is "Advanced options" link at the bottom of add/edit flatpage admin panel (eg. http://127.0.0.1:8000/admin/flatpages/flatpage/add/. There you can enable comments, enable registraion requirement and change template.

Programmatically, you can do:

from django.contrib.flatpages.models import FlatPage
my_flat_page = FlatPage.objects.get(title="example")
my_flat_page.template_name = "/flatpages/example.html"
my_flat_page.save()
#or just:
#from django.contrib.flatpages.models import FlatPage
#FlatePage.objects.filter(title="example").update(template_name="/flatpages/example.html")
#if you like one-liners
2
On

Sure! After installing flatpages per the instructions here, you can override the default template (flatpages/default.html) for any given flatpage -- specifically, you can customize instances of the FlatPage model (esp. the template_name field, but not just that), either programmatically like for any other model, or, I believe, in the admin page.