Search code examples
djangodjango-flatpages

Specific templates for flatpages in Django


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'),

Solution

  • 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