Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a specific flatpage in a template

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)

like image 569
djq Avatar asked Nov 17 '15 21:11

djq


1 Answers

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.

like image 148
GwynBleidD Avatar answered Sep 27 '22 22:09

GwynBleidD