Flexible link block and field for Wagtail CMS
Project description
wagtail-link-field
A flexible link block and field for Wagtail CMS that supports internal pages, external URLs, documents, anchors, email, phone, and custom links.
Features
- LinkBlock: A StreamField block for flexible link selection
- LinkField: A model field that stores link data as JSON
- LinkPanel: A panel for editing LinkField in Wagtail admin
- Template tags: For rendering links in templates
- Dynamic field visibility: JavaScript shows/hides relevant fields based on selected link type
- Clean JSON storage: Only relevant fields are stored, irrelevant ones are stripped
Installation
pip install wagtail-link-field
Add to your INSTALLED_APPS:
INSTALLED_APPS = [
# ...
"wagtail_link_field",
# ...
]
Usage
LinkBlock in StreamField
from wagtail.fields import StreamField
from wagtail import blocks
from wagtail_link_field import LinkBlock
class MyPage(Page):
body = StreamField([
("link", LinkBlock()),
], blank=True, use_json_field=True)
# Restrict to specific link types
body_restricted = StreamField([
("link", LinkBlock(link_types=["internal-link", "external-link"])),
], blank=True, use_json_field=True)
LinkField with LinkPanel
from django.db import models
from wagtail_link_field import LinkField, LinkPanel
class MyPage(Page):
cta = LinkField(blank=True, null=True)
content_panels = Page.content_panels + [
LinkPanel("cta"),
# Or restrict link types:
LinkPanel("cta_restricted", link_types=["internal-link", "external-link"]),
]
Accessing link data
page = MyPage.objects.first()
# Get the URL
url = page.cta.url() # Returns the computed URL
# Check if external
is_external = page.cta.is_external() # True for external, email, phone, custom links
# Get the action type
action = page.cta.action # e.g., "internal-link", "external-link"
Template tags
{% load wagtail_link_field_tags %}
{# Render a full anchor tag #}
{% render_link page.cta css_class="btn btn-primary" %}
{# Or use filters for custom markup #}
<a href="{{ page.cta|link_url }}" class="my-class">{{ page.cta|link_title }}</a>
Available link types
| Type | Description | Fields |
|---|---|---|
internal-link |
Link to a Wagtail page | internal_link, page_query_string |
document-link |
Link to a Wagtail document | document_link |
external-link |
External URL | external_link |
anchor-link |
Same-page anchor | anchor_link |
email-link |
Email address | url_link (auto-prefixed with mailto:) |
phone-link |
Phone number | url_link (auto-prefixed with tel:) |
custom-link |
Custom URL scheme | url_link |
Restricting link types
Pass link_types to LinkBlock or LinkPanel:
# Only internal and external links
LinkBlock(link_types=["internal-link", "external-link"])
# Only documents
LinkPanel("download", link_types=["document-link"])
Optional links
Set required=False for optional links:
# LinkBlock (default is required=True)
LinkBlock(required=False)
# LinkField (use blank=True, null=True)
cta = LinkField(blank=True, null=True)
Development
Setup
# Clone the repo
git clone https://github.com/infofactory/wagtail-link-field.git
cd wagtail-link-field
# Install with uv
uv sync --extra testing
Running tests
# Run tests
python testmanage.py test
# Run with coverage
python -m coverage run testmanage.py test
python -m coverage report -m
Running linting
ruff check .
ruff format --check .
Running tox (matrix testing)
tox
License
BSD-3-Clause. See LICENSE for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wagtail_link_field-0.1.0.tar.gz.
File metadata
- Download URL: wagtail_link_field-0.1.0.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a16edb4fab1aebad63bbb4b286499fd7526c42f10aaf4a7da24b541c99aa65f
|
|
| MD5 |
24a8102331b989630881e13b55e53553
|
|
| BLAKE2b-256 |
f6a0308c189ceab7505efd704237f81fe772c4596b9578586b129ece5458ebb0
|
Provenance
The following attestation bundles were made for wagtail_link_field-0.1.0.tar.gz:
Publisher:
python-publish.yml on infofactory/wagtail-link-field
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wagtail_link_field-0.1.0.tar.gz -
Subject digest:
1a16edb4fab1aebad63bbb4b286499fd7526c42f10aaf4a7da24b541c99aa65f - Sigstore transparency entry: 1109501901
- Sigstore integration time:
-
Permalink:
infofactory/wagtail-link-field@8743c280b9704283481f21f39ea036a5b45e7cc6 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/infofactory
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8743c280b9704283481f21f39ea036a5b45e7cc6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file wagtail_link_field-0.1.0-py3-none-any.whl.
File metadata
- Download URL: wagtail_link_field-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
474d294a2fbb1d6ad070463c3aba74fe8248a45c66d883dd72efb7abe5cd9bbc
|
|
| MD5 |
bf667e0a66b61a0dda504e791f918be8
|
|
| BLAKE2b-256 |
1b1ca539472b0a3c7e9bdd025fcf1f6e8fbd5084a76543e354be96591c041e8c
|
Provenance
The following attestation bundles were made for wagtail_link_field-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on infofactory/wagtail-link-field
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wagtail_link_field-0.1.0-py3-none-any.whl -
Subject digest:
474d294a2fbb1d6ad070463c3aba74fe8248a45c66d883dd72efb7abe5cd9bbc - Sigstore transparency entry: 1109501906
- Sigstore integration time:
-
Permalink:
infofactory/wagtail-link-field@8743c280b9704283481f21f39ea036a5b45e7cc6 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/infofactory
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8743c280b9704283481f21f39ea036a5b45e7cc6 -
Trigger Event:
release
-
Statement type: