Flatpages in Django

Web applications often contain pages which don’t change frequently. For example, About Us, EULA (End User License Agreement), Terms of Use, etc. Such pages are known as flatpages. Django provides a built-in app named django.contrib.flatpages for handling flatpages.

Installing Flatpages App

The flatpages app (django.contrib.flatpages) is not enabled by default. To install it open settings.py file and add django.contrib.flatpages to the INSTALLED_APPS list as follows:

djangobin/django_project/django_project/settings.py

The flatpages app depends upon another app called Sites framework (django.contrib.sites) which is also not enabled by default. To enable sites framework add django.contrib.sites to the INSTALLED_APPS setting and define a SITE_ID variable with the value of 1 at the end of the settings.py file.

djangobin/django_project/django_project/settings.py

Now run migrate command as usual to create necessary tables.

The Flatpage Model

The flatpages are represented using FlatPage model in the database. Here is is how the FlatPage model looks like:

Let’s examine each field one by one.

  1. url (required): This field represents the URL where flatpage will appear. It can only take URL which begins and end with leading and trailing slash. For example, /about/, /eula/, /about-us/ etc.
  2. title (required): Title of the flatpage.
  3. content (optional): Content of the flatpage in HTML.
  4. enable_comments (optional): A Boolean field which designates whether you want to enable comments for the flatpage or not. Note that setting this field to True will not automatically create comment box in the flatpage. If you want to have a comment form you have to code it yourself. We only use this field as a flag in templates to check whether comments are enabled for a flatpage or not.
  5. template_name (optional): template to use for rendering flatpages. If not given it will fall back to default template flatpages/default.html.
  6. registration_required (optional): A Boolean field, if set to True then only logged in users will be able to view the flatpage.
  7. sites (required): This field is very important. Every flatpage is associated with a site using many-to-many relationship. In other words, a FlatPage object can belong to one or more Site object.

Sites Framework

Sites framework (django.contrib.sites) allows us to run multiple websites from same database and Django project. Sites framework provides a model called Site which contains the following two fields:

  1. domain
  2. name

domain – It is used to specify the is the address of the website. For example, example.com or http://example.com.

name – This field is commonly used to specify a human-readable easy to remember the name of the site.

While enabling sites framework we have defined SITE_ID variable with the value of 1 because this is our first site.

Django automatically adds a record with domain and name field set to example.com, when we run migrate command after adding django.contrib.sites to INSTALLED_APPS in settings.py file.

That’s all you need to know about the sites framework.

Having learned the anatomy of flatpages, let’s create some flatpages. At this point, there are two ways to create flatpages.

  1. Django admin site.
  2. Django ORM.

Let’s try creating flatpages via Django admin site.

Creating flatpages using Django admin site

Visit http://127.0.0.1:8000/admin/ and login into the Django admin site.

You should see Flat Page and Sites listed with all the other installed apps as follows:

Click the “Add” link in front of Flat Page to create a new FlatPage. You should see Add flat page form like this:

Create an “About” page by entering the following details:

As discussed, the url, title and sites fields are required.

Notice Advanced options section at the bottom of the form. Click on the Show link to make it visible. The Advanced section allows you to set values for the following fields:

  • registration_required
  • template_name

We don’t need to enter any data into these fields for now.

Once you are done hit Save button at the bottom of the page.

Creating flatpages using Django ORM

Flatpages are implemented as FlatPage model. To use FlatPage model we have to first import it from django.contrib.flatpages.models module as follows:

At this point, we have a single flatpage named “About” in our database. To get it inside the shell, type the following code:

Let’s create an EULA page.

Now we have two flat pages “About” and “EULA”. Unlike _About_ page, _EULA_ page is not usable at this point because it is not associated with any Site object. Consider the following:

The above code demonstrates that our “About” page is associated with one site (i.e example.com), However, “EULA” page is associated with none.

To make EULA flatpage usable, connect it to a Site object as follows:

Creating template for flatpages

We now have two flatpages but no way of displaying them. By default, a FlatPage object uses flatpages/default.html template unless you specify a different one while creating/updating a FlatPage object.

Django doesn’t supply flatpages/default.html template, so you have to create one yourself. In the djangobin’s templates directory, create a new directory named flatpages. Then inside the flatpages directory create a new file called default.html and add the following code to it.

djangobin/django_project/blog/templates/flatpages/default.html

Notice the flatpage variable used in the template. All flatpage templates are passed a context variable named flatpage which is a FlatPage object. Once we have the FlatPage object we can access any relevant information by typing a (.) dot followed by the attribute name. In our case, we are only outputting the value of title and content attribute of the FlatPage object.

URL Pattern

Everything is in place now. We just need some URL patterns to access the flatpages. Open djangobin’s urls.py and add the following two URL patterns at the end of the urlpatterns‘s list.

djangobin/django_project/blog/urls.py

The flatpage app (django.contrib.flatpages) provides a flatpage() view whose job is to render the flatpages. It accepts a required argument called url which is the URL of the template we entered while creating the flatpage.

Open djangobin’s base.html and add links to _About_ and _EULA_ page as follows:

djangobin/django_project/djangobin/templates/djangobin/base.html

Open your browser and visit http://127.0.0.1:8000/about/ and http://127.0.0.1:8000/eula/. You should see _About_ and _EULA_ page as follows:

Leave a Comment