13. Generic pages

Robert Crowther Mar 2022

PrevNext

Most straightforward website—websites that provide web‐pages—need generic pages. Pages for contact, company details, ‘about’—that kind of page. Django has a solution—the (optional) app called ‘flatpages’.

Flatpages

The app has these features,

You see that flatpages includes most of the feature you could want for such an app, so it is worth considering. That said…

Ref

Generic app

You could make your own app for flatpages. I stand there are two reasons. First, you would like something very generic, which not only uses the same CSS and even HTML layout, but is of the project, perhaps inheriting models. Second, you may want some special model structure, such as Richtext Fields or extended data like SEO. Otherwise go with the Flatpages app.

If you want to make your own app, here’s a reminder how to make an app, lightly customised for generic pages. Modify as you need,

./manage.py startapp sitepages

Register, in siteName/settings.py.

INSTALLED_APPS = [
    'sitepages.apps.SitepagesConfig',
]

Make a model,

from django.db import models
from django.utils.translation import gettext_lazy as _
from django.urls import reverse



class SitepageBase(models.Model):

    date = models.DateField(_("Date of entry creation"),
        editable=False,
        auto_now_add=True,
    )

    title = models.CharField("Title",
        max_length=255, blank=False,
        db_index=True,
        unique=True,
    )

    slug = models.SlugField(_('slug'),
        allow_unicode=True,
        max_length=255,
        help_text=_("The name of the page as it will appear in URLs e.g http://domain.com/blog/[my-slug]/")
    )

    content = models.TextField("Content",
        blank=True,
        default=""
    )

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('sitepage_detail', kwargs={'slug': self.slug})

Migrate,

./manage.py makemigrations sitepages
./manage.py migrate sitepages

Roll some admin,

from django.contrib import admin
from .models import Sitepage



class SitepagesAdmin(admin.ModelAdmin):
    # fields=['title', 'slug', 'content']
    prepopulated_fields = {"slug": ("title",)}

admin.site.register(Sitepage, SitepagesAdmin)

Make a view, in views.py,

from django.shortcuts import render
from django.views.generic import DetailView
from sitepages.models import Sitepage



class SitepagesView(DetailView):
    # Change the name of the object in the template
    context_object_name = 'sitepage'
    # Allow all objects to be shown
    model = Sitepage
    # Allow some objects to be shown, or re-ordered etc.
    #queryset = Review.objects.all()

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        #print(context)
        return context

Make a template, in sitepages/templates/sitespages/,

{% extends "base.html" %}
{% load tml %}

{% block content %}
    <article class="sitepage-typeblock">
        <h1>{{sitepage.title}}</h1>
        <div class="teaser">{{ sitepage.content|tml_uml }}</div>
    </article>

{% endblock %}

Wire the urls into siteName/urls.py,

from sitepages.views import SitepagesView

    path('<slug:slug>/',  SitepagesView.as_view(), name='sitepage_detail'),

Put this entry near the bottom of the list, it’s a short URL.