Using a specific page in a template

I am using django flatpage s and I am wondering if there is a compressed way to load one specific page into a template. Documents 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 into a template, essentially using it as an include statement (for example, {% include 'homepage.html' %} )

The approach I'm using is this:

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

This works great, but I thought it might be a slightly tidier way to do this. I mark the content as safe since I want to use html styles (again, not sure if there is a better way to do this)

+6
source share
1 answer

By default, there are 2 solutions for this.

First, register your page in the URLs as shown below:

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

Thus, you can access it using the {% url %} tag, as a normal view.

Secondly, if you know the exact URL of this page, you can get the URL to it using:

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

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

+2
source

All Articles