Django Oscar - Flatpages - Part 27

In this part, we will create simple pages in Django Oscar using the dashboard.

These pages are useful for content that does not need a custom model or custom view.

  • About
  • Shipping policy
  • Returns
  • Terms and conditions
  • Privacy policy

 

Flatpages

Django Oscar can work with Django flatpages.

First, make sure these apps are installed:

INSTALLED_APPS = [
    ...

    "django.contrib.sites",
    "django.contrib.flatpages",

    ...
]

Also add:

SITE_ID = 1

 

Middleware

Add the flatpage middleware near the end of your middleware list.

MIDDLEWARE = [
    ...

    "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware",
]

This allows Django to try to find a flatpage when no other URL matches.

 

Migrations

python manage.py migrate

 

Site configuration

Go to the Django admin:

/admin/sites/site/

Make sure your site is configured correctly.

For local development, something like this is enough:

Domain name: localhost:8000
Display name: localhost

 

Creating a page

Now go to the Oscar dashboard:

Dashboard > Content > Pages

Create a new page:

  • URL: /about/
  • Title: About
  • Content: Your page content
  • Sites: localhost

Save it and visit:

http://localhost:8000/about/

 

Template

By default, Django flatpages will look for this template:

mysite/templates/flatpages/default.html

{% extends "oscar/layout.html" %}
{% load i18n %}

{% block title %}
    {{ flatpage.title }}
{% endblock %}

{% block content %}
    <div class="container py-4">
        <h1>{{ flatpage.title }}</h1>

        <div class="mt-3">
            {{ flatpage.content|safe }}
        </div>
    </div>
{% endblock %}

 

Custom template

When creating a page, you can also define a custom template.

Example:

Template name: flatpages/about.html

Then create:

{% extends "oscar/layout.html" %}

{% block title %}
    {{ flatpage.title }}
{% endblock %}

{% block content %}
    <div class="container py-4">
        <h1>{{ flatpage.title }}</h1>

        <p>This is a custom about page template.</p>

        <div>
            {{ flatpage.content|safe }}
        </div>
    </div>
{% endblock %}

 

Adding the page to the frontend menu

If you want to link this page from your frontend navigation, add a normal link:

<a href="/about/">About</a>

Or using Django's url system is not necessary here, because flatpages are usually defined by their URL path in the dashboard.

 

Book store example

For our book store, useful pages could be:

  • /about/
  • /shipping/
  • /returns/
  • /terms/
  • /privacy/

 

Simple rule

Use flatpages for simple content pages managed from the dashboard.

If the page needs complex logic, forms, database queries, or custom permissions, create a normal Django view instead.