Skip to main content

karni-site

Public websites for Karni: every text, image and menu editable in the admin, public forms, redirects, and error pages that cannot fail.

PyPI License: Apache-2.0 Docs

A site app is plain project code: function views, templates and static files, written by karni make site, karni make view and karni import design. What every such site needs, and no project should write again, lives here. See Public server-rendered pages.

Install

pip install "karni-site[images]"
karni add site
karni migrate

The images extra is Pillow. Without it everything works, and uploaded images are shown as uploaded: no smaller copies and no upload cap (see {% image %} below).

A new project of a kind with a site (karni new "Name" --kind site, or --design <folder> for a designer's pages) has the module wired from the start. karni make site <app>, karni import design and karni make form do the karni add site step for you when the project does not have the module yet, and an app becomes a site+app (kind under [tool.karni] in pyproject.toml). Then install it (uv pip install -r pyproject.toml, or rebuild the container) and migrate: the module's tables hold what the client edits.

Wiring, which karni add site and karni make site write into config/settings.py:

  • "karni_site" in the # karni:apps block,
  • "karni_site.middleware.RedirectMiddleware" in the # karni:middleware block (the admin's redirects, SITE005 without it),
  • "karni_site.context_processors.site" in the template engine's context processors (site in every template, SITE004 without it), in a block of its own:
TEMPLATES[0]["OPTIONS"]["context_processors"] += [  # noqa: F821
    "karni_site.context_processors.site",
]  # karni:context_processors

and into config/urls.py, before the site's own include, the module's URLs (robots.txt, sitemap.xml, and the view every form posts to):

path("", include("karni_site.urls")),

karni make site also sets the error handlers in config/urls.py (and karni make form adds the include when it is missing):

handler404 = "karni_site.views.page_not_found"
handler500 = "karni_site.views.server_error"

Main features

  • {% editable %} and {% editable_image %}: every text and image editable in the admin, with the template's text as the default and a history
  • {% get_menu %} menus in the design's own markup, and the site's name, logo and contact details as site
  • Public forms with {% karni_form %}, with and without JavaScript, under the enforced Content-Security-Policy
  • {% karni_seo %} for titles, descriptions, share tags and JSON-LD, plus robots.txt and sitemap.xml
  • Redirects, and 404 and 500 pages that fall back to a plain page when one fails
  • {% image %} serves uploads as WebP copies at the size the page shows them

What it gives a site

Load the tags in every template that uses them: {% load static karni_site %}. A template that extends another does not inherit its loads.

Everything a client sees, edited in the admin

Every word and image of a public site, its menus, its contact details and its page titles can be changed in the admin, without the project modelling each paragraph. karni import design <dir> --app <app> --editable marks all of it in a designer's pages (see Public server-rendered pages). The same tags work in a template written by hand:

The client changes In the admin In the template
a text Site, Page content {% editable "home.intro" %}Bread worth the walk.{% endeditable %}
an image Site, Page content {% editable_image "home.hero" %}<img src="..." alt="...">{% endeditable_image %}
a menu Site, Menus {% get_menu "header" as items %}{% for item in items %}...{% endfor %}
the name, logo, contact details, opening hours, social links Site, Site settings {{ site.name }}, {% site_image "logo" %}...{% endsite_image %}, {{ site.phone }}
an old address Site, Redirects nothing: the middleware answers it
a page's title, description and share image Site, Page titles and descriptions {% karni_seo %} in base.html

The template holds the design's text and images as the defaults. The first time a template renders, they are stored. A block nobody changed follows the template: a new version of the design changes it. A block someone changed keeps their version. Deleting a block hands it back to the template, and so does the admin action Reset to the template's text or image. Every change someone makes is in the History tab (karni.audit). Storing a template's default, or following a new one, is nobody's edit and leaves no entry there.

manage.py karni_site_seed: the defaults stored in a deploy

Without it, the first render of a page is a visitor's request, and that request stores each new block, the menu its seed gives and the site's details. Run the command in a deploy, after migrate, and no visitor's request writes:

python manage.py karni_site_seed

It requests every page the way karni check --seo finds them (/, sitemap.xml, the named URLs without arguments and every page they link to, up to KARNI_SITE_SEO_MAX_PAGES), through Django's test client in its own process, and stores what the templates give that the database does not hold yet. A block or menu item nobody edited takes the template's new default. It prints what it stored, and fails with SITE024 when a page answers a server error (--keep-going reports those and exits 0). The release step of karni make deploy compose and deploy.sh of karni make deploy systemd-nginx run it in a project with this module.

It seeds whatever KARNI_SITE_SEED_ON_RENDER says. With the command in the deploy, KARNI_SITE_SEED_ON_RENDER = False makes a visitor's request write nothing at all. A page the command does not reach (one only a form or a signed-in user opens, a page behind a capture that no link or sitemap entry names) then shows its template's defaults without storing them, so the client cannot edit them until a run reaches it. Leave the setting on to have such pages store their defaults on their first visit.

site: the site's own details

SiteSettings is one row: name, tagline, logo, favicon, share_image (the default image when a page is shared), address, phone, phone_link (the number a tap dials, +44..., when the phone is written another way), email, map_url, and as rows below them the opening hours (days, times) and the social links (name, url). The context processor puts it in every template as site:

<a href="{{ site.phone_href }}">{{ site.phone }}</a>
<a href="{{ site.email_href }}">{{ site.email }}</a>
{% for line in site.address_lines %}{{ line }}<br>{% endfor %}
<dl>{% for row in site.hours %}<dt>{{ row.days }}</dt><dd>{{ row.times }}</dd>{% endfor %}</dl>
{% if site.social.instagram %}<a href="{{ site.social.instagram.url }}">Instagram</a>{% endif %}
{% for link in site.social_links %}<a href="{{ link.url }}" class="icon-{{ link.slug }}">{{ link.name }}</a>{% endfor %}

The design's values are the defaults, given once at the top of the layout:

{% load static karni_site %}{% site_defaults name="Crumb & Co" phone="01234 567890" email="hello@example.com" social_instagram="https://www.instagram.com/crumb" %}

hours="Monday to Friday=8:00 to 18:00|Saturday=9:00 to 13:00|Sunday=Closed" seeds the opening hours rows: each | a row, the days before =, the times after. They are added when the site has none and follow the template while they are as it gave them. Once the client changes or deletes them, theirs stay.

The logo, the favicon and the default share image take a static path (share_image="website/img/share.jpg", or any file field of the settings model): the field stays empty, and {% karni_seo %} (the share image and the JSON-LD's logo) and the admin's preview use that file until the client uploads one. karni_site.sitesettings.image_url(site, "share_image") gives the address the same way for your own code.

A field is filled the first time and follows the template until someone changes it in the admin. A field the client empties stays empty, and a social link they delete is not added again. {% site_image "logo" %}<img ...>{% endsite_image %} shows the design's image until a logo is uploaded, then the same <img> with the new address. {% site_image "favicon" url %}...{% endsite_image %} holds a bare address, for <link rel="icon" href="...">. Without a row, site is an empty one: a template never fails on {{ site.name }}.

A project that needs more fields subclasses karni_site.models.AbstractSiteSettings and names its model in KARNI_SITE_SETTINGS_MODEL before karni_site's first migrate (the way AUTH_USER_MODEL works). The admin, site and the tags follow it, and the admin shows the new fields under More.

{% editable %} and {% editable_image %}: text and images

<h1>{% editable "home.hero.title" %}Bread from <mark>our own oven</mark>{% endeditable %}</h1>
{% editable_image "home.hero.photo" %}<img class="hero" src="{% static 'website/hero.jpg' %}" srcset="..." alt="A lake at dawn" loading="lazy">{% endeditable_image %}
<section style="background-image: url(&quot;{% editable_image "home.band" url %}{% static 'website/band.png' %}{% endeditable_image %}&quot;)">

A key is lowercase words joined by dots, the page first: the admin's Page content groups blocks by that first part, in the order the page shows them. The admin edits a text block as plain text: a new line is a line break, and the highlight convention is typed as a marker (==every scale==). Bold, emphasis and links are kept as <b>, <em> and <a href>.

An edited block is cleaned when it is saved (karni_site.sanitize, built on the standard library's html.parser): it keeps the elements KARNI_SITE_INLINE_TAGS lists with their listed attributes, removes script, style, iframe, svg and the like with their content, every other element with its text kept, every event attribute (onclick), and any link whose scheme KARNI_SITE_LINK_SCHEMES does not list (javascript:, data:). A link that opens a new tab gets rel="noopener noreferrer". The template's own default is shown as written, with the words between the highlight markers in the highlight's markup, the way an edited text shows them.

The highlight convention is KARNI_SITE_HIGHLIGHT: the marker a client types, and the element and class it becomes. The default is ==words== as <mark>. karni import design --editable sets it to the design's own markup when its headings highlight words (<span class="highlight">, or <em> without a class). It is one convention for the whole site: a model field rendered with {{ value|highlight }} reads the same marker. A backslash before the marker (\==) keeps it as typed.

An uploaded image keeps the design's markup: the <img> keeps its classes, loading hints and every other attribute, its src (and alt) change, and srcset, sizes and <source> go because they point at the design's files. Uploads are checked by extension and by their first bytes (KARNI_SITE_IMAGE_TYPES). SVG is not accepted by default, because an SVG file can carry script that runs when someone opens its address.

A page's blocks are one cache entry, read once per request: a page with fifty blocks costs one cache read, and saving a block clears its page's entry. When the database cannot be read, the template's default is shown.

{% get_menu %}: menus in the design's own markup

{% get_menu "header" seed="website/menus/header.html" as items %}
<ul class="nav-links">
  {% for item in items %}
  <li{% if item.children %} class="has-sub"{% endif %}>
    <a href="{{ item.url }}"{% if item.active %} class="active"{% endif %}{% if item.new_tab %} target="_blank" rel="noopener"{% endif %}>{{ item.title }}</a>
    {% if item.children %}<ul class="sub-menu">{% for child in item.children %}<li><a href="{{ child.url }}">{{ child.title }}</a></li>{% endfor %}</ul>{% endif %}
  </li>
  {% endfor %}
</ul>

Each item has title, url, new_tab, current (this page), active (this page, a page under the item's own path, or one of its children: Treatments at /treatments/ stays active on /treatments/whitening/, and the home page / stands for no section) and children. KARNI_SITE_MENU_SECTIONS = False leaves out the pages under the path. An item in the admin goes to one of the site's pages (its URL name, so a changed path follows), a row's own page (its get_absolute_url()), or an address (https://..., #section, mailto:), with an optional #section or ?query after it. Children from adds one child per row of a model, so a submenu of services follows the services the client adds. An item with children from and neither a title nor an address lists the rows in its own place: a footer's list of every service is one such item. A menu is any name: header, footer, footer-services.

seed names a template holding the design's own navigation. The first time the menu is asked for and the database has none of that name, the links in it (nested lists becoming children) become the menu, so the client starts with the designer's menu. A deleted menu is seeded again.

The menu then follows its seed, the way a block follows its template's text. When the seed's links change (a new link, a new text, a link to another page, such as a view that was renamed), each item the seed made and nobody changed takes its link's new text, target and place, an item whose link left the seed goes, and a new link becomes an item. An item someone changed in the admin (its title, where it goes, its place, shown or not), an item someone added and an item someone deleted stay as they are. A menu someone made in the admin, without a seed, never follows one. KARNI_SITE_MENU_FOLLOW_SEED = False seeds a menu once and leaves it, and karni_site.menus.sync_menu(name, markup, source=...) follows a seed from code of your own. A menu seeded before Karni 0.6.0 follows too: the items nobody saved since the seed made them follow, and the others are kept.

A list in the seed marked data-karni-children-from="website.Service" becomes an item with children from that model instead of its links (in its parent's place when it is the whole menu), and keeps its links when the project has no such model. karni import design --editable --map marks a list whose links are exactly one model's detail pages.

A menu's tree is cached. Saving a menu or an item clears it, and so does saving a row of a model an item points at.

Redirects

karni_site.middleware.RedirectMiddleware answers the redirects of the admin's Redirects: an old address (from the first slash, /old-page/) and the new one, moved for good (301) or moved for now (302). A query string on the request is passed on. An old address with a query string (/index.php?page=contact) matches only that query. By default a redirect answers only where the site has no page (a 404), so a page that exists is never redirected and costs nothing. With KARNI_SITE_REDIRECTS_BEFORE_VIEWS = True every request is looked up before its view, one cache read each. A redirect that would send visitors in a circle is refused when it is saved (SITE006), and so is one from the admin's, the API's or the static files' addresses.

Page titles and descriptions

Page titles and descriptions (PageSeo) holds a page's title, description, share image and hide from search engines, for one of the site's pages by URL name (every page of that view) or for one path. The lookup is karni_site.seo.page_seo(request): the entry for the exact path first, then the one for the URL name, or None. Answers are cached, and a save clears them. {% karni_seo %} renders them.

{% karni_seo %}: what search engines and sharing sites read

In base.html's <head>, around the design's own title and description:

{% karni_seo %}<title>{% block title %}{{ site.name }}{% endblock %}</title>
<meta name="description" content="{% block description %}{% endblock %}">{% block seo %}{% endblock %}{% endkarni_seo %}

It renders the page's <title>, <meta name="description">, a canonical link (the page's address without its query string, on KARNI_SITE_URL when it is set), <meta name="robots" content="noindex"> for a page hidden from search engines, the Open Graph tags (og:type, og:site_name, og:title, og:description, og:url, og:locale, og:image with its size and alt text) and the Twitter card, the site's JSON-LD, and one CSS rule that keeps the images Karni sized in proportion. Each value is the first of:

  1. the page's entry in the admin's Page titles and descriptions;
  2. seo in the template's context, a dict a view passes: render(request, "website/search.html", {"seo": {"noindex": True}}) (title, description, image, image_alt, type, noindex, and canonical, None for none);
  3. the row the page is about: seo or object in the context when it is a row, else the row in the context whose get_absolute_url() is the page's path. Its fields are what class Karni: seo names (seo = {"title": "name", "description": "intro", "image": "photo"}), else a title in seo_title, title or name, a description in seo_description, description, summary, excerpt, intro (and the like), an image in seo_image, image, photo (and the like). A title from a row is written KARNI_SITE_TITLE_FORMAT ("{title} | {site}") unless it names the site, and a description from a row loses its HTML and is cut at KARNI_SITE_DESCRIPTION_LENGTH characters;
  4. the tags written inside the tag: the design's <title>, its description, an og:image, twitter:card or og:type (a page's own go in {% block seo %});
  5. the site's settings: its name, its tagline, its default share image.

A share image is absolute, the copy closest above 1200 pixels wide when the image is an upload with copies. A tag inside that the tag does not write (theme-color, twitter:site, a design's own JSON-LD) stays where it is. The tag never fails a page: if anything goes wrong, the tags inside are rendered as written. The error views pass seo={"noindex": True, "canonical": None}, so a 404 page is never indexed.

The JSON-LD is a WebSite and an Organization, or a LocalBusiness when the site has opening hours (KARNI_SITE_SCHEMA_TYPE, which also takes any schema.org type, "Bakery", or None for none), from the site's settings: name, tagline, address, phone, email, map link, logo, share image, social profiles (sameAs) and, for a business, its opening hours. Hours are written as the site shows them ("Mon to Fri", "9:00 to 17:00", "9am - 5.30pm", "Closed"). English day names and these time forms are read. A line it cannot read is left out rather than guessed. KARNI_SITE_JSONLD names your own (request, site) -> dict function for anything else.

sitemap.xml and robots.txt

/sitemap.xml lists the site's pages (every named URL without arguments, except the admin's, the API's, the static files', karni_site's own and redirects) and the rows of every model with a page of its own (get_absolute_url()), from its default manager, so scopes apply and soft-deleted rows stay out. A model chooses its public rows:

class Karni:
    sitemap = {"filter": {"status": "live"}, "lastmod": "edited_at", "changefreq": "weekly"}

Without a filter, the first boolean field KARNI_SITE_PUBLISHED_FIELDS names (published, is_published, is_public, public, visible, is_visible) must be true, a status with a published choice must be published, and a published_at must have passed. sitemap = False leaves a model out.

A view behind a capture with a known set of addresses and no model (/topics/<slug:slug>/ for a few topics) declares them, and they join the pages section and the page test (experimental):

from karni_site.sitemaps import pages


@pages({"slug": "tides"}, {"slug": "harbours"})  # or a function that returns them
def topic(request, slug): ...

karni_site.sitemaps.declared_pages() lists their paths: assert_pages(client, [*PAGES, *declared_pages()]). A page or row hidden from search engines in the admin is left out, and so is what KARNI_SITE_SITEMAP_EXCLUDE names (URL names or paths, wildcards allowed). Karni's own routes (the health and readiness probes and the welcome page) are never listed, here or in the menu item's "goes to" choices: add your own technical routes with KARNI_SITE_TECHNICAL_ROUTES. lastmod is updated_at by default. Past 50,000 addresses in a section, sitemap.xml becomes an index of /sitemap-<section>.xml?p=<page>.

/robots.txt closes the site to every crawler unless indexing is allowed: by default only where DJANGO_ENV is prod, so a development or staging copy is never indexed. KARNI_SITE_ALLOW_INDEXING = True or False decides for every environment. While indexing is allowed it names the sitemap and asks crawlers to stay out of KARNI_SITE_ROBOTS_DISALLOW. A karni_site/robots.txt template of the project's own replaces it.

robots.txt is a request a proxy can rewrite: a CDN may put its own Allow: / in front of the file. So while indexing is not allowed, every page says it too. The redirect middleware adds X-Robots-Tag: noindex, nofollow to every response Django sends (a view that sets the header keeps its own), and {% karni_seo %} writes <meta name="robots" content="noindex, nofollow">. KARNI_SITE_ROBOTS_TAG holds the directives, and "" sends neither. Static and media files are served by the web server, not Django: add the header in its configuration when they must stay out of search results too. karni check --seo checks the pages as an open site serves them, so a development copy is judged as production will be.

{% image %}: uploads at the size the page shows them

{% image project.photo width=400 alt=project.title class="card-img" %}
{% image page.hero alt="" eager=True %}
{% for slide in slides %}{% image slide.image width=1920 alt=slide.title eager=forloop.first %}{% endfor %}

renders an <img> with srcset and sizes from the image's WebP copies, width and height in its proportions, loading="lazy" and decoding="async". width is the width the page shows it at (its own without one). alt is required, alt="" for an image that only decorates. Any other name=value is an attribute, an underscore a hyphen in data_ and aria_ names. A lazy image's sizes starts with auto, so the browser loads the copy that fits the layout: a photo in a card at the card's size. An empty field renders nothing. eager=True is for the picture at the top of a page, the one that paints largest: it loads first (loading="eager" and fetchpriority="high"), and in a loop eager=forloop.first does that for the first item only, such as the first slide of a hero slider.

The copies are made when a save uploads an image (an ImageField, or one of this module's image fields), by a Celery task, through the field's own storage, local or S3: a WebP copy at each of KARNI_SITE_IMAGE_WIDTHS below the image's width ([320, 480, 640, 800, 960, 1280, 1600, 1920]) and one at its own width unless it is a WebP already, at KARNI_SITE_IMAGE_QUALITY (80), named <name>.<width>w.webp next to the original, and a manifest, <name>.renditions.json. An original wider or taller than KARNI_SITE_IMAGE_MAX_SIZE (2560) is scaled down first, turned upright from its EXIF orientation and without its EXIF data. A page that shows an image without copies (uploaded before the module, or loaded in bulk) shows the original, never an error, and queues them, once per ten minutes per image. {% editable_image %} and {% site_image %} use the copies of an uploaded picture the same way, keeping the design's width.

Without Pillow nothing is scaled or copied: the original is shown as uploaded, with its width and height, which are read from the file's first bytes and need no Pillow.

karni check --seo

karni check --seo renders every page of the site through Django's test client, against the project's database: /, the sitemap's addresses, the named URLs without arguments and every page they link to, up to KARNI_SITE_SEO_MAX_PAGES. A page's title, description and images come from the database and from template inheritance, so only a rendered page shows what a search engine reads. Rendering stores a template's defaults, as a first visit would. It reports:

Code Finding
SITE010 a page without a title
SITE011 a title two pages search engines may index share
SITE012 a page without a description
SITE013 a description two such pages share
SITE014 an <img> without alt
SITE015 an <img> without width and height
SITE016 an image without smaller copies that is wider than KARNI_SITE_SEO_PIXEL_RATIO times its width, wider than the widest copy, or heavier than KARNI_SITE_SEO_IMAGE_BYTES
SITE017 a link, image, stylesheet or script of the site that answers an error
SITE018 no sitemap.xml, a page the site links to that it leaves out, or an address in it that does not answer 200 or asks not to be indexed
SITE007 a menu item whose page, row or model is gone (a view renamed or replaced since the menu was seeded), which the menu leaves out

An upload a page shows without its smaller copies gets them first, made in the check's own process, and the check names those images: in development no Celery worker may have made them yet, and the check judges what the site serves once one has. The site itself always queues the copies (in a project with CELERY_TASK_ALWAYS_EAGER = True the first page that shows the image makes them and uses them). KARNI_SITE_QUEUE_RENDITIONS = False makes none in the check either. KARNI_SITE_SEO_IGNORE lists paths it never requests.

The crawl's own requests are not logged (KARNI_SITE_SEO_QUIET names the loggers held back: the request log, the query log, Django's request warnings), so the findings are not buried, and the development capture under .karni/ keeps the developer's requests. An error a page's request logs is counted, and one line names the first of them.

The editor role

The module declares a role, editor, through Karni's role system (the karni.roles entry point, merged under KARNI_ROLES): view and change the site settings, the page content, the menus and the page titles. No enquiries, users, roles, redirects or project models. karni sync-roles (and every migrate) creates it. Give a person staff status and the role. Karni's admin pages open for whoever the roles' grants admit, so no Django permission needs ticking, and the permission matrix changes the role's grants without a deploy.

{% ifactive %}: the current page in a menu

<a href="{% url 'website:about' %}"{% ifactive "website:about" %} class="active"{% endifactive %}>About</a>
<a class="nav-link{% ifactive "website:services" "website:service_*" %} active{% endifactive %}" href="...">Services</a>
<li{% ifactive "/blog/*" %} aria-current="page"{% else %} class="idle"{% endifactive %}>...</li>

The tag renders its content when one of its patterns names the current page, and its {% else %} part otherwise. It knows nothing about the markup, so it fits any design: put it around whatever the design uses for the current page, a class, a token inside a class, or a whole attribute. A pattern is a URL name ("website:about", with shell wildcards: "website:service_*") or a path ("/about/", "/blog/*"). A variable holding a name or a list works too. Without the request in the context nothing is current.

karni import design writes these tags from the design itself: on each link, the pages where the designer marked it current. When a model's detail pages become one view (karni make view project --path "/work/<slug:slug>/"), the command rewrites each {% ifactive %} that named the old pages' views to the folder's path ("/work/*"), and karni check (SITE002) names any left naming a view that is gone.

{% csp_hash %} and {% csp_allow %}: the design under the Content-Security-Policy

Karni sends a Content-Security-Policy on every response (security headers). It runs no inline script and loads nothing from another origin. A design's inline scripts and its fonts or scripts from a CDN would stop working in production, where the policy is enforced. The tags allow exactly what the template holds, for the response being rendered only:

{% csp_allow "style-src https://fonts.googleapis.com" "font-src https://fonts.gstatic.com" %}
<script>{% csp_hash %}document.documentElement.classList.add("js");{% endcsp_hash %}</script>
<button onclick="{% csp_hash attribute %}history.back(){% endcsp_hash %}">Back</button>
  • {% csp_hash %} renders its content unchanged and adds its SHA-256 hash to script-src. The markup stays as the designer wrote it, and an edited script is hashed again on the next render, so an edit cannot break the page in production only. Put it around the text inside <script>, nothing else. A data block (type="application/ld+json") needs no hash.
  • {% csp_hash attribute %} is for an event handler attribute (onclick, onload): it adds 'unsafe-hashes' and the hash of the decoded value.
  • {% csp_allow "<directive> <source> ..." %} adds sources. A template that loads something from another origin says so next to the markup that loads it. It renders nothing.

Never add 'unsafe-inline' to script-src for a site. If a project does, the hashes are left out of the policy, because a browser that sees a hash ignores 'unsafe-inline'.

{% karni_form %}: public forms, with and without JavaScript

karni make form contact name:str email:email message:text --notify ENQUIRY_RECIPIENTS writes the project's side: the model (the visitor's fields, a status lifecycle and staff-only notes that field_roles hides from visitors), a BaseViewSet with create=Public(), the tests, the staff app's pages and, with --notify, a queued email. The page then needs one tag:

{% load karni_site %}
{% karni_form "contact" %}
{% karni_form "contact" classes="bootstrap" button="Send message" %}
{% karni_form "booking" rows="name,email" labels="day: Preferred day" widgets="service: radios"
   field_class="field" row_class="form-row" button_class="btn btn-accent" %}

The tag renders the fields a visitor fills: the writable fields of the viewset's create serializer, as an anonymous visitor sees them, so a signed-in staff member sees the public's form too. Each field gets its label, a required mark, its limits (maxlength, min, max, step), its choices, its help text and a browser autofill hint. The form carries the CSRF token and the viewset's honeypot field, and loads karni_site/forms.js once per page.

Without JavaScript the browser posts to karni_site:form (forms/<name>/). The view saves through the viewset's own create, so the access declaration, the public-create rate limit, the honeypot, validation and change history are the API's. A sent form redirects (303) back to the page with ?sent=<name>, where the tag shows the success message. The page is the hidden karni-next field, else the Referer, else / (never the form's own address, which answers 405 to a GET). A refused one renders the page again, by its own view, with the values and each field's error next to it (400, 429 when rate-limited, 403 when not allowed). A filled honeypot looks sent and saves nothing.

With JavaScript forms.js posts the same data with fetch, asks for JSON, and shows the answer in place: the success message, the inline errors (aria-invalid, aria-describedby, the first one focused), the rate-limit message. It is a static file from the site with no inline code, so it runs under the enforced Content-Security-Policy as it is.

Argument Meaning
fields="name,email" which fields, in this order (default: every one a visitor fills)
rows="name,email|phone,company" fields that share a row element
labels, placeholders, widgets "field: text | field: text". Widgets: input, textarea, select, radios, checkboxes
attrs="message: rows=4 | guests: min=1 max=8" attributes of a field's control, as the design sets them: rows, cols, min, max, step, pattern, size, inputmode, autocomplete (single quotes around a value with spaces). The form's own name, type, value, required and class cannot be replaced (SITE021)
button, success, invalid, throttled, refused, failed, choose the texts
next="/thanks/" where a sent form goes (default: back to this page)
id the form's id (default karni-form-<name>)
classes="bootstrap" a preset of class names (KARNI_SITE_FORM_PRESETS)
<part>_class the class of one part: form, row, column, field, label, input, select, check, check_input, check_label, invalid, error, help, message, success, failure, button, required
template another template for the whole form
editable="contact.enquiry" each field's label and placeholder, the button and the sent message are editable blocks under that key (contact.enquiry.email.label, contact.enquiry.button), the texts above as their defaults

The name is the model's ("contact" for Contact, "booking_request" for BookingRequest, "shop.enquiry" when two apps have one), served by the viewset the API router registered for it. KARNI_SITE_FORMS maps any other name to a viewset. A name no form has fails karni check (SITE020).

When the site came from karni import design, karni make form names the template and line of the design's <form> it replaces and prints the tag with the arguments that keep that form's own class names, rows, labels, placeholders, button and control attributes (attrs=). It matches the design's controls to the form's fields by name, then by label and type, says which control fills which field, and adds editable= after an editable import. karni make form <name> --tag prints the tag again and writes nothing.

notify_recipients: the email a new submission sends

@on_created(Contact, queued=True)
def notify_contact(contact: Contact) -> None:
    notify_recipients(
        contact,
        setting="ENQUIRY_RECIPIENTS",
        template="website/email/contact",
        reply_to=contact.email,
    )

The listener is project code (karni make form --notify writes it), so a project can add a condition or another channel. queued=True sends from a Celery worker after the row is committed. notify_recipients reads the addresses from the setting (a list, or one comma-separated string), renders the project's <template>_subject.txt and <template>_body.txt (and _body.html when it exists) with submission, fields, verbose_name, admin_url, admin_path and frontend_url, collapses the subject to one line and sets Reply-To. admin_url is the row's admin page as a full address: on KARNI_SITE_URL when it is set, else on the host the visitor's request came to, which the queued task carries (karni.tracing.get_origin). The body karni make form writes leaves out each optional field the visitor left empty. An empty setting sends nothing and logs a warning. A missing one raises SITE023.

highlight: words the design sets apart

<h2>{{ project.heading|highlight }}</h2>                  {# Custom homes, <span class="accent">crafted for place</span> #}
<h1>{{ slide.heading|highlight:"em" }}</h1>               {# another markup for this value #}
<title>{{ project.heading|highlight:"" }}</title>          {# the words, without the marks #}

A design often highlights some words of a heading. karni design extract keeps them between the site's marker in the field (Custom homes, ==crafted for place==), so staff edit a model field in the admin the way a client edits a block: one convention, KARNI_SITE_HIGHLIGHT, for the whole site. Without an argument the filter renders the marked words in the setting's tag and class (with {"marker": "==", "tag": "span", "class": "accent"} above). A spec names another markup for one value: a tag with optional classes ("span.dot", "em"), and the marker stays the setting's. "" drops the marks. Everything else is escaped, and a backslash before the marker (\==) keeps it as typed. A spec that is not a tag fails the render with SITE003. karni_site.text.highlight is the same function for Python code, and karni_site.sanitize.apply_marks the one editable blocks use.

Error pages that cannot fail

karni_site.views.page_not_found and server_error render the site's own 404.html and 500.html with the request, so the layout's links, menu and policy tags work there too. Django's own 500 view renders without the request. If the page raises while it renders (a database that is down, a tag that fails), the next template in the list is tried. The last is this module's plain karni_site/500.html (or 404.html), which needs nothing.

Settings

Every setting arrives through the module's settings fragment (karni_site.conf.DEFAULTS) and is overridable in the project's settings.py. The ones that differ between deployments of a project are read from the environment too, the way Karni's own are: KARNI_SITE_URL, KARNI_SITE_ALLOW_INDEXING, KARNI_SITE_QUEUE_RENDITIONS, KARNI_SITE_SEED_ON_RENDER, KARNI_SITE_ROBOTS_TAG and KARNI_SITE_NOTIFY_FROM, set in .env or .env.prod (the production examples karni make deploy writes list them). Leave a variable unset for its default: an empty one is a value. settings.py wins over the environment.

Setting Default Meaning
KARNI_SITE_IS_ACTIVE "karni_site.navigation.is_active" The (request, pattern) -> bool function {% ifactive %} and menu items ask for each pattern.
KARNI_SITE_NOT_FOUND_TEMPLATES ["404.html", "karni_site/404.html"] The templates page_not_found tries, in order.
KARNI_SITE_SERVER_ERROR_TEMPLATES ["500.html", "karni_site/500.html"] The templates server_error tries, in order.
KARNI_SITE_SETTINGS_MODEL "karni_site.SiteSettings" The model site is. Swap it before karni_site's first migrate.
KARNI_SITE_HIGHLIGHT {"marker": "==", "tag": "mark", "class": ""} What a client types around words to highlight them, in an editable block and in a field rendered with `{{ value
KARNI_SITE_INLINE_TAGS b, strong, i, em, br, and a with href, title, target, rel The elements an edited block keeps, each with the attributes it keeps. The highlight element joins them.
KARNI_SITE_LINK_SCHEMES ["http", "https", "mailto", "tel"] The schemes a link in an edited block may use. A relative link always may.
KARNI_SITE_SANITIZER "karni_site.sanitize.sanitize" The (html) -> html function that cleans an edited block.
KARNI_SITE_IMAGE_TYPES ["jpg", "jpeg", "png", "gif", "webp", "avif", "ico"] The image types an upload may be, checked by extension and content.
KARNI_SITE_CACHE "default" The cache alias blocks, menus, redirects, page SEO entries and the settings are kept in.
KARNI_SITE_CACHE_TIMEOUT 3600 Seconds a cached entry lives. Every save clears what it changed, so this is a backstop.
KARNI_SITE_SEED_ON_RENDER True Whether the first render of a template stores its defaults (blocks, a menu's seed, the site's details). manage.py karni_site_seed stores them whatever this says.
KARNI_SITE_MENU_ROWS "karni_site.menus.menu_rows" The (model, item) -> rows function a children from item lists. The default is the model's default manager, so its scopes apply.
KARNI_SITE_MENU_ROWS_LIMIT 100 At most this many rows per children from item.
KARNI_SITE_MENU_SECTIONS True Whether a menu item is active on every page under its own path too (/services/ on /services/web/). The home page never is.
KARNI_SITE_MENU_FOLLOW_SEED True Whether a menu seeded from a template follows it: the items nobody changed take the seed's new links. False seeds it once.
KARNI_SITE_REDIRECTS_BEFORE_VIEWS False True looks every request up before its view, so a redirect also replaces a page that exists.
KARNI_SITE_EDITOR_ROLE "editor" The slug of the role this module declares, or None for none.
KARNI_SITE_FORMS {} Form name to a viewset's dotted path or a model label, for a name that is not a model's.
KARNI_SITE_FORM_CLASS "karni_site.forms.SiteForm" The class that describes, renders and submits a form.
KARNI_SITE_FORM_TEMPLATE "karni_site/forms/form.html" The form's template. Each field renders karni_site/forms/field.html, or karni_site/forms/<form>/<field>.html or karni_site/forms/<form>/field.html when one exists.
KARNI_SITE_FORM_CLASSES {} Class names merged over the plain defaults (karni-form, karni-field, ...).
KARNI_SITE_FORM_PRESETS {} Named class sets for classes="...", merged over the shipped bootstrap.
KARNI_SITE_FORM_MESSAGES {} The texts, merged over the defaults: success, invalid, throttled, refused, failed, choose, button.
KARNI_SITE_FORM_REQUIRED_MARK "*" The text after a required field's label.
KARNI_SITE_FORM_SCRIPT True Whether the tag loads karni_site/forms.js. False keeps the form working without it.
KARNI_SITE_FORM_SENT_PARAM "sent" The query parameter a page gets after a form was sent.
KARNI_SITE_FORM_PAGE_TEMPLATE "karni_site/forms/page.html" The page that shows a refused form when its own page cannot be rendered again.
KARNI_SITE_NOTIFY_FROM "" The sender of notify_recipients emails. Empty: DEFAULT_FROM_EMAIL.
KARNI_SITE_URL None The site's address, "https://www.example.com", for canonical links, share tags, the sitemap and robots.txt. None: the address each request came to.
KARNI_SITE_ALLOW_INDEXING None Whether search engines may index the site. None: only where DJANGO_ENV is prod.
KARNI_SITE_ROBOTS_DISALLOW [] Paths robots.txt asks crawlers to stay out of while indexing is allowed.
KARNI_SITE_ROBOTS_TAG "noindex, nofollow" What every page says while indexing is not allowed: the X-Robots-Tag header and the robots meta tag of {% karni_seo %}. "" sends neither.
KARNI_SITE_SITEMAP_EXCLUDE [] URL names and paths the sitemap leaves out, wildcards allowed.
KARNI_SITE_TECHNICAL_ROUTES [] URL names and paths added to Karni's own technical routes, which neither the sitemap nor the menu's "goes to" choices list.
KARNI_SITE_SITEMAPS "karni_site.sitemaps.sitemaps" The () -> {name: Sitemap} function that lists the sitemap's sections.
KARNI_SITE_PUBLISHED_FIELDS ["published", "is_published", ...] Boolean fields that mark a row public, for a model in the sitemap without a filter.
KARNI_SITE_SCHEMA_TYPE "auto" The JSON-LD type: "auto" (LocalBusiness with opening hours, else Organization), any schema.org type, or None.
KARNI_SITE_JSONLD "karni_site.jsonld.build" The (request, site) -> dict function that builds the JSON-LD.
KARNI_SITE_SEO_OBJECT "karni_site.metadata.page_object" The (context, request) -> object function that finds the row a page is about.
KARNI_SITE_TITLE_FORMAT "{title} | {site}" How a title from the page's row is written.
KARNI_SITE_DESCRIPTION_LENGTH 160 The longest description taken from a row, in characters.
KARNI_SITE_IMAGE_WIDTHS [320, 480, 640, 800, 960, 1280, 1600, 1920] The widths of an upload's WebP copies (and its own width). [] makes none.
KARNI_SITE_IMAGE_QUALITY 80 Their WebP quality.
KARNI_SITE_IMAGE_MAX_SIZE 2560 An upload wider or taller is scaled down to fit. None keeps it.
KARNI_SITE_QUEUE_RENDITIONS True Whether an upload, and a page that shows an image without copies, queue them. With KARNI_SINGLE_PROCESS the queue runs in the process, so the copies are made without a worker.
KARNI_SITE_RENDITIONER "karni_site.renditions.generate" The (name, storage) -> Manifest function that makes the copies.
KARNI_SITE_IMAGE_STYLE height: auto for img[data-karni-image] The CSS {% karni_seo %} writes for the images Karni sized. "" leaves it out.
KARNI_SITE_SEO_MAX_PAGES 500 karni check --seo requests at most this many pages.
KARNI_SITE_SEO_IMAGE_BYTES 500000 An image without smaller copies above this size is oversized (SITE016).
KARNI_SITE_SEO_PIXEL_RATIO 2 An image wider than this many times its width is oversized.
KARNI_SITE_SEO_IGNORE [] Regular expressions of paths karni check --seo never requests.
KARNI_SITE_SEO_QUIET ["django_structlog", "django.request", "django.server", "karni.queries"] The loggers karni check --seo holds back while it requests the pages. Their errors are counted and the first is shown. [] keeps every line.

Checks

karni check runs six checks while karni_site is installed:

  • SITE001: the template engine passes the request to templates (django.template.context_processors.request). The tags need it.
  • SITE002: every URL name a {% ifactive %} names exists, so a renamed view cannot leave a menu item that is never marked.
  • SITE004: the template engine puts site in every template (karni_site.context_processors.site).
  • SITE005: the redirect middleware is installed.
  • SITE008: no block key has two different defaults (or is text in one template and an image in another), which would store the block again on every render.
  • SITE020: every form a {% karni_form %} names exists.

karni check --seo runs SITE010 to SITE018 (see above). A tag written wrongly fails when the template compiles, with SITE003 (SITE021 for {% karni_form %}, SITE019 for {% karni_seo %}, {% image %} and a wrong class Karni: sitemap or seo). A redirect in a circle is refused with SITE006, a menu item that points nowhere with SITE007, and a KARNI_SITE_SETTINGS_MODEL that names no installed model fails with SITE009. A form whose view is not routed raises SITE022, and a notification naming a setting the project lacks SITE023. A page that fails while manage.py karni_site_seed renders it stops that command with SITE024.

Escape hatches

  • KARNI_SITE_SETTINGS_MODEL: the site's settings with fields of your own.
  • KARNI_SITE_SANITIZER, KARNI_SITE_INLINE_TAGS, KARNI_SITE_LINK_SCHEMES and KARNI_SITE_HIGHLIGHT: what an edited block keeps.
  • KARNI_SITE_SEED_ON_RENDER = False: nothing is written while a page renders. Store the defaults with manage.py karni_site_seed in the deploy, or load blocks, menus and settings with karni load.
  • KARNI_SITE_MENU_ROWS: which rows a children from item lists.
  • KARNI_SITE_MENU_FOLLOW_SEED = False: a seeded menu never follows its seed, and karni_site.menus.sync_menu() follows one when you choose.
  • KARNI_SITE_EDITOR_ROLE, a role of that slug in KARNI_ROLES, or the permission matrix: the editor's grants.
  • Unregister any of this module's admins and register your own subclass. Every Karni admin page opens for a grant, so your own models' pages do too (KARNI_ADMIN_GRANTS_OPEN_PAGES, admin).
  • karni_site.blocks.text() / image(), karni_site.menus.entries(), karni_site.redirects.redirect_for() and karni_site.seo.page_seo() for your own tags, views or middleware.
  • KARNI_SITE_IS_ACTIVE: another rule for the current page.
  • KARNI_SITE_NOT_FOUND_TEMPLATES, KARNI_SITE_SERVER_ERROR_TEMPLATES, or template_name= on either view. Or point handler404/handler500 at your own views.
  • KARNI_CSP adds sources site-wide, and @csp_extend per view, instead of the tags. karni.security.extend_request_csp(request, {...}) is what the tags call, for your own tag or view code.
  • Forms: the arguments of {% karni_form %}, the KARNI_SITE_FORM_* settings, the templates karni_site/forms/form.html and field.html (per form and per field too), KARNI_SITE_FORMS for any viewset, KARNI_SITE_FORM_CLASS for a karni_site.forms.SiteForm subclass, and the viewset's own hooks (access, throttle, get_honeypot_field(), perform_create). Route your own view as karni_site:form to replace the one the form posts to.
  • Notifications: edit the email templates or the listener, or pass to=, from_email= and context= to notify_recipients.
  • {% karni_seo %}: seo in a view's context, KARNI_SITE_SEO_OBJECT, class Karni: seo, KARNI_SITE_TITLE_FORMAT, KARNI_SITE_JSONLD and KARNI_SITE_SCHEMA_TYPE. karni_site.metadata.resolve() returns the values for a tag of your own.
  • The sitemap: class Karni: sitemap, KARNI_SITE_SITEMAP_EXCLUDE, KARNI_SITE_PUBLISHED_FIELDS, or KARNI_SITE_SITEMAPS. robots.txt: KARNI_SITE_ALLOW_INDEXING, KARNI_SITE_ROBOTS_DISALLOW or a karni_site/robots.txt template. A closed site's header and meta tag: KARNI_SITE_ROBOTS_TAG, or RedirectMiddleware.robots() in a subclass. Or a route of your own for either path before the include.
  • Images: KARNI_SITE_IMAGE_WIDTHS, KARNI_SITE_IMAGE_QUALITY, KARNI_SITE_IMAGE_MAX_SIZE, KARNI_SITE_RENDITIONER, and KARNI_SITE_QUEUE_RENDITIONS = False with karni_site.renditions.make() where you choose. karni_site.seo_tags.image_tag() builds the markup in Python.
  • KarniSiteConfig: subclass it and point INSTALLED_APPS at the subclass. Keep the label karni_site.

Metadata

Release files for karni-site 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for karni-site 0.7.0
File Size Uploaded Provenance Actions
karni_site-0.7.0.tar.gz 166.1 kB Without provenance Details for karni_site-0.7.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for karni-site 0.7.0
File Interpreter ABI Platform Actions
karni_site-0.7.0-py3-none-any.whl Python 3 none any Details for karni_site-0.7.0-py3-none-any.whl

Total release size: 343.4 kB

Release files / karni_site-0.7.0.tar.gz

Download URL karni_site-0.7.0.tar.gz
Size 166.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bad54ecc729c747e7af6e2589a7333b1e505cc3f486fe45dddcc0b2e909005ae
BLAKE2b-256 checksum
How to use checksums
faf8ef366211d5e59e4895c55eb03bceb174fb6b42341e62e2577378136bc27d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / karni_site-0.7.0-py3-none-any.whl

Download URL karni_site-0.7.0-py3-none-any.whl
Size 177.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8bfc165ec8674d35ccc7a1cb3ad35039b76662f1540a5f7af1203340222a7f0c
BLAKE2b-256 checksum
How to use checksums
14a8196e24f771e3869d51fd4e6f7d91b703d69fd1c7d694d900bab3671a0b7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for karni-site 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page