Metadata-Version: 2.5
Name: ggds-django
Version: 0.4.0
Summary: Ghana Government Design System (GGDS) for Django: page parts as template tags, a form renderer, and the GGDS styles and script.
Project-URL: Documentation, https://github.com/ghana-gds/ggds
License-Expression: Apache-2.0
License-File: LICENSE
License-File: LICENSE.md
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.0
Classifier: Framework :: Django :: 6.0
Requires-Python: >=3.10
Requires-Dist: django>=5.0
Description-Content-Type: text/markdown

# ggds-django

The [Ghana Government Design System](https://github.com/ghana-gds/ggds) (GGDS) for Django: the page parts as
template tags, a form renderer that turns any Django form into GGDS markup with correct error handling, the GGDS
styles, themes and script as static files, and GGDS colours and header for Django admin. Django 5.0 or later. Works
with Wagtail and other Django-based CMSs.

**Documentation:** [ghana-gds.github.io/ggds](https://ghana-gds.github.io/ggds/) · [Use it with Django](https://ghana-gds.github.io/ggds/get-started/developers.html#django) · [Versions and support](https://ghana-gds.github.io/ggds/versions.html)

## Set up

```python
INSTALLED_APPS = [..., "django.contrib.staticfiles", "ggds_django"]
FORM_RENDERER = "ggds_django.forms.GGDSFormRenderer"   # every form in GGDS markup
TEMPLATES = [{..., "OPTIONS": {"context_processors": [..., "django.template.context_processors.request"]}}]
```

To use GGDS for some forms only, leave `FORM_RENDERER` out and add `GGDSFormMixin` to those forms.

## A page

```django
{% load ggds %}<!doctype html>
<html lang="en-GB">
<head>
  <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
  <title>{% ggds_error_title form "Make a complaint" %} | Example Agency</title>
  {% ggds_assets theme="<theme>" %}
</head>
<body>
{% ggds_skip_link %}
{% ggds_banner short_name="EA" host="example.gov.gh" official=is_official %}
{% ggds_header name="Example Agency" nav=nav %}
<main id="main" tabindex="-1" class="ggds-container ggds-main">
  {% ggds_breadcrumbs crumbs %}
  <h1>Make a complaint</h1>
  <form method="post">{% csrf_token %}
    {{ form }}
    <button class="ggds-button">Send complaint</button>
  </form>
</main>
{% ggds_footer name="Example Agency" sections=sections links=footer_links %}
{% ggds_tab_bar tabs %}
</body>
</html>
```

Links are dicts (`{"label": "Services", "href": "/services/"}`) or `("Services", "/services/")` pairs. The header
marks the section the page's address is in; the tab bar marks the current tab.

### Or extend GGDS's page

Put your details in settings once, and every page extends `ggds/page.html`: the skip link, banner, header,
`<main>`, footer and tab bar come in GGDS's order, so a newer ggds-django updates the whole frame.

```python
TEMPLATES = [{..., "OPTIONS": {"context_processors": [..., "django.template.context_processors.request",
                                                      "ggds_django.context_processors.site"]}}]
GGDS_SITE = {
    "name": "Example Agency", "short_name": "EA", "host": "www.example.gov.gh", "theme": "<theme>",
    "purpose": "We license communications services.", "nav": [...], "sections": [...], "tabs": [...],
    "links": [{"label": "Accessibility", "href": "/accessibility/"}, ...],   # the footer's bottom bar
    "address": "1 Example Road, Accra", "email": "info@example.gov.gh", "phone": "030 000 0000",
}
```

```django
{% extends "ggds/page.html" %}
{% block title %}Make a complaint | Example Agency{% endblock %}
{% block content %}<h1>Make a complaint</h1>…{% endblock %}
```

Other blocks: `head` (your own CSS and scripts), `main_class`, `scripts`, and one per part to replace it. A view can
mark the header link with `ggds_current` and the tab with `ggds_tab` in its context. `npx @ggds/create --stack
django` makes a whole website this way.

## Forms

With the renderer, `{{ form }}` shows an error summary after a failed submit (focused on load by the GGDS
script, each item linking to its field, a radio group's to its first option), then every field:

| Django widget | GGDS markup |
|---|---|
| Text, email, number, date… | `ggds-field` with label, hint (`help_text`), error and `ggds-input` |
| `Textarea`, `Select`, `FileInput` | the same with `ggds-textarea`, `ggds-select`, `ggds-file-upload` |
| `RadioSelect`, `CheckboxSelectMultiple` | a `ggds-fieldset` with a legend and `ggds-choices` |
| `CheckboxInput` | a single `ggds-choice` |
| `GGDSDateField` | a `ggds-fieldset` with day, month and year boxes (`ggds-date-input`) |
| `PasswordInput` | a GGDS password input: the GGDS script adds the Show button; `autocomplete="current-password"` unless the widget says `new-password` |
| `OneTimeCodeField` | a GGDS code input (`ggds-input--code`, `autocomplete="one-time-code"`) |
| `AffixInput(prefix="GH₵")` | a text input with a unit before (`prefix`) or after (`suffix`) it, hidden from screen readers |

Each control gets `aria-describedby` (hint, then error) and, with an error, `aria-invalid="true"`. Errors start
with a hidden "Error:". Fields that are not required say "(optional)". Set `autocomplete` on personal details with
the widget's `attrs`. One field: `{{ form.phone.as_field_group }}`.

### Dates people know

```python
from ggds_django.forms import GGDSDateField

class ApplicationForm(forms.Form):
    date_of_birth = GGDSDateField(label="What is your date of birth?", name="date of birth", birthday=True,
                                  help_text="For example, 27 3 1990")
```

`cleaned_data["date_of_birth"]` is a `datetime.date`. The month can be typed as 3, 03, March or Mar. Errors use
`name`: "Enter date of birth", "Date of birth must include a year", "Year must include 4 numbers", "Date of birth
must be a real date", and with `birthday=True` or `past=True`, "Date of birth must be in the past". Only the boxes
with the problem are marked invalid, and the error summary links to the first of them. `birthday=True` also adds
`autocomplete="bday-day"` and so on.

### Codes and amounts

```python
from ggds_django.forms import AffixInput, OneTimeCodeField

class PayForm(forms.Form):
    amount = forms.DecimalField(label="How much do you want to pay, in Ghana cedis?", widget=AffixInput(prefix="GH₵"))

class CodeForm(forms.Form):
    code = OneTimeCodeField(label="Enter the code we sent you", digits=6)
```

`OneTimeCodeField` removes spaces and dashes, keeps leading zeros, and says "Enter the code we sent you" or "Enter
the 6-digit code we sent you".

## Page parts

| Tag | |
|---|---|
| `{% ggds_assets theme="<theme>" %}` | Stylesheet, theme and optional script, in `<head>` |
| `{% ggds_skip_link %}` | First thing in `<body>` |
| `{% ggds_banner short_name host official %}` | Government website banner |
| `{% ggds_header name nav %}` | Logo or initials, name, sections, Search |
| `{% ggds_hero title purpose lede search_action tasks %}` | Homepage hero |
| `{% ggds_breadcrumbs crumbs %}` | Trail ending with the current page |
| `{% ggds_summary_list rows %}` | Check answers: rows of `{"key", "value", "href"}`; an empty value shows "Not given", and each Change link says what it changes. `plain=True` for no lines |
| `{% ggds_panel "Complaint sent" reference=ref %}` | Confirmation panel: the page's `<h1>` and the reference number |
| `{% ggds_page_heading "Welcome back, Ama" eyebrow="..." lede="..." %}` | Page heading: the top of an inner page in the institution's colour, with the page's `<h1>`. `danger=True` for a warning in force |
| `{% ggds_key_figures figures %}` | Key figures: dicts of `{"value", "label"}`, and optionally `"change"` (in words) and `"danger": True`. `inverse=True` on a page heading |
| `{% ggds_progress 3 4 %}` | Progress: "Question 3 of 4" in words and as a native `<progress>`. `label="Step 2 of 4: your canoe"` to change the words |
| `{% ggds_timeline stages %}` | Timeline: dicts of `{"title", "when", "state"}`, where state is `"done"`, `"current"` or `""`; each stage says its state to screen readers |
| `{% ggds_meter value max=1100 %}` | Meter: an amount as a bar, for a table cell next to the number; hidden from screen readers |
| `{% ggds_steps steps %}` | Steps: dicts of `{"icon", "title", "text"}`. `urgent=True` for an emergency; `heading_level=2` if they sit under the `<h1>` |
| `{% ggds_pagination page_obj %}` | From Django's `Paginator`; keeps other query parameters |
| `{% ggds_footer name purpose address email phone sections links %}` | Footer |
| `{% ggds_tab_bar tabs %}` | Phone quick links: each tab has an `icon`. The current tab is the request's path, or `current=` |
| `{% ggds_error_summary form %}` | On its own, for forms you lay out yourself |
| `{% ggds_back_link href %}` | Back to the previous page of a task. `label=` only if "Back" would be unclear |
| `{% ggds_phase_banner "beta" feedback_href %}` | Under the header for a new service (`"alpha"` or `"beta"`) |
| `{% ggds_side_nav items %}` | The pages of one section; the current page is the request's path, or `current=` |
| `{% ggds_contents items %}` | "On this page": links to the page's own headings |
| `{% ggds_language_switcher %}` | The same page in each language Django is set up for, or `links=` |
| `{% ggds_cookie_banner "Example Agency" choice %}` | Asks before setting analytics cookies; works without JavaScript |
| `{% ggds_task_list tasks %}` | Task list: dicts of `{"label", "href", "status", "hint"}`, where status is `"completed"`, `"inProgress"`, `"notStarted"` or `"cannotStart"` (no link) |
| `{% ggds_dialog id title body action confirm warning=True %}` | A dialog that confirms an action, Cancel first; its form posts to `action` with the CSRF token. Open it with a link to `action` that has `data-ggds-dialog="<id>"` |
| `{% ggds_timeout_warning timeout=1200 keep_alive=... sign_out=... %}` | Timeout warning on every signed-in page: match `timeout` to `SESSION_COOKIE_AGE` and set `SESSION_SAVE_EVERY_REQUEST = True`, so each page extends the session as the warning counts. `keep_alive` is a view that sets `request.session.modified = True`, and redirects or fails once the session has ended |
| `{% ggds_feedback action="/feedback/" report_url="/report-a-problem/" %}` | "Did this page help you?" at the end of a page: Yes and No post to `action` with the CSRF token and the page's path; `answered=True` says thank you |
| `{% ggds_text "key" %}`, `{% ggds_icon "name" %}` | GGDS's own words in the active language, and any GGDS icon |

Everything else (buttons, notices, tags, cards, tables) is plain GGDS HTML: copy it from the documentation.

## Django admin

Admin keeps its own layout but takes GGDS's colours, type, focus style and header (the flag-colour rule and the
institution's logo or initials, name and country), from the GGDS tokens and your institution's theme:

```python
INSTALLED_APPS = [..., "ggds_django", "ggds_django.admin_theme", "django.contrib.admin", ...]  # above admin
GGDS_ADMIN = {
    "theme": "<theme>",                             # a file in ggds/themes/, as for {% ggds_assets %}
    "name": "Example Agency",                        # default: the admin site's site_header
    "initials": "EA",                               # or "logo_url" and "logo_alt"
    "country": "Republic of Ghana",                 # the default; "" for none
}
```

It loads the tokens on their own (`ggds/tokens.css`), never the GGDS base styles, which would restyle admin's
elements. Admin is shown light only: the dark palette and the theme toggle are left out. `manage.py check` warns if
the app is below `django.contrib.admin`, a key is misspelt or the theme does not exist.

## Content added after load

HTMX, fetch or a script that adds a GGDS menu or error summary should call `initAll(element)` from the GGDS script
(`ggds/js/index.js`) on the new content.

## Develop

From the GGDS repository: `pnpm run build` copies the tokens, styles, themes, script and icons into the app;
`pnpm run test:django` runs the tests (including a check that the markup matches the GGDS documentation) and renders
an example site into the documentation build, where the accessibility tests check it.
