8

I'm using django flatpages, and I'm wondering if there is a concise way of loading one specific flatpage within a template. The docs show the following patterns:

{% load flatpages %}

{% get_flatpages '/about/' as about_pages %}
{% get_flatpages about_prefix as about_pages %}
{% get_flatpages '/about/' for someuser as about_pages %}

I just want to load one specific page in a template, essentially using it like an include statement (e.g. {% include 'homepage.html' %})

The approach I am using is this:

{% get_flatpages '/flat-homepage/' as flatpages %}
{{ flatpages.first.content|safe }}

This works fine, but I thought there might be a slightly neater way of doing this. I'm marking the content as safe as I want html styles to be applied (again, not sure if there is a better way of doing this)

Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52
djq
  • 14,810
  • 45
  • 122
  • 157

1 Answers1

2

By default, there are 2 solutions for that.

First, register your flatpage in urls as below:

from django.contrib.flatpages import views

urlpatterns = [
    url(r'^about-us/$', views.flatpage, {'url': '/about-us/'}, name='about'),
]

That way, you can access it using {% url %} tag, like normal view.

Second way, if you know exact url of that page, you can access it's url using:

{% url 'django.contrib.flatpages.views.flatpage' url='url_to_flatpage_here' %}

There is no other way, because all flatpages are identified by url.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77