views:

326

answers:

2

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'),
+1  A: 

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.

Alex Martelli
It's not possible to do it in admin - I think that feature was removed. How can I do this programmatically?
Eeyore
Get the flatpage you desire (search in Flatpages.object for title="about", etc), set its `template_name` attribute to your desired value ("flatpages/about.html" etc), save it. Just like any other model, as I said!
Alex Martelli
+3  A: 

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
Daniel Hernik