wagtail-metadata
This plugin adds custom properties to your page models and then lets you output meta-attribute tags using the included template tag. These tags help with search engine optimisations and for creating nice shareable links for social media, mainly Facebook and Twitter.
Compatibility
Wagtail-metadata works with Wagtail v5.0 and upwards. For compatibility with older versions of Wagtail, see past releases.
Installing
First, install using pip:
pip install wagtail-metadata
Then add wagtailmetadata to your project’s INSTALLED_APPS:
INSTALLED_APPS = [
'home',
'search',
# etc...
'wagtail.contrib.settings',
'wagtail.contrib.modeladmin',
# etc...
# Add the following:
'wagtailmetadata',
]
Using
Pages should inherit from wagtailmetadata.models.MetadataPageMixin. This provides a search_image field in the Wagtail interface for that page type. The description for the page will be pulled from the search_description page. Metadata for the page will then be built from the page details.
from wagtail.core.models import Page
from wagtailmetadata.models import MetadataPageMixin
class ContentPage(MetadataPageMixin, Page):
pass
If you want to use this with a non-page model, or want to use a different implementation for the fields, you can inherit from wagtailmetadata.models.MetadataMixin. You will need to implement the following methods:
from wagtailmetadata.models import MetadataMixin
class CustomObject(MetadataMixin, object):
def get_meta_title(self):
"""The title of this object"""
return "My custom object"
def get_meta_url(self):
"""The URL of this object, including protocol and domain"""
return "http://example.com/my-custom-object/"
def get_meta_description(self):
"""
A short text description of this object.
This should be plain text, not HTML.
"""
return "This thing is really cool, you should totally check it out"
def get_meta_image_url(self, request):
"""
Return a url for an image to use, see the MetadataPageMixin if using a Wagtail image
"""
return 'https://neonjungle.studio/share.png'
def get_meta_twitter_card_type(self):
"""
What kind of Twitter card to show this as.
Defaults to ``summary_large_photo`` if there is a meta image,
or ``summary`` if there is no image. Optional.
"""
return "summary_large_photo"
If your custom object uses Wagtail images, you may wish to use the intermediary mixin wagtailmetadata.models.WagtailImageMetadataMixin so you can use the relationship for the image related metadata:
from django.db import models
from wagtailmetadata.models import WagtailImageMetadataMixin
class CustomObject(WagtailImageMetadataMixin, object):
share_image = models.ForeignKey('wagtailimages.Image', ondelete=models.SET_NULL, null=True, related_name='+')
def get_meta_image(self):
return self.share_image
Display
Django
To use this in a template, first load the template tag library, and then insert the metadata by placing {% meta_tags %} into the <head>:
{% load wagtailmetadata_tags %}
{% meta_tags %}
By default, this will look for a self object in the context to pull the metadata from. You can specify a different object to use if you need to:
{% load wagtailmetadata_tags %}
{% meta_tags my_custom_object %}
Jinja2
Add wagtailmetadata.jinja2tags.WagtailMetadataExtension to the template extensions in your settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'OPTIONS': {
'extensions': [
'wagtailmetadata.jinja2tags.WagtailMetadataExtension'
],
},
}
]
Insert the metadata by placing {{ meta_tags() }} into the <head>:
{{ meta_tags() }}
By default, this will look for a page object in the context to pull the metadata from. You can specify a different object to use if you need to:
{{ meta_tags(my_custom_object) }}
Troubleshooting
‘meta_tags’ missing request from context
The template that is trying to render the meta_tags tag does not have a request object in the context.
‘meta_tags’ tag is missing a model or object
There was no model passed to the template tag, or self is not found in the current context.
Release files for wagtail-metadata 5.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| wagtail-metadata-5.0.0.tar.gz | 10.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wagtail_metadata-5.0.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 22.8 kB
Release files / wagtail-metadata-5.0.0.tar.gz
| Download URL | wagtail-metadata-5.0.0.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b9109928bad53306b21b64cf9205d84cbb88fc625bac0a38305bebb9c26e64a
|
|
BLAKE2b-256 checksum How to use checksums |
44adf77f4d394eec667236db9c7d80c12c3f721b495373c4616539dd3fc3bfa5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.13
|
Release files / wagtail_metadata-5.0.0-py2.py3-none-any.whl
| Download URL | wagtail_metadata-5.0.0-py2.py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
fcc2bbb71e83b353ceef05d69a4cde3f2fbe645c9e5035e46f7829fc234a6405
|
|
BLAKE2b-256 checksum How to use checksums |
6a5c0326104a7606c076a99eb59cb0fb200e22fc54b449a71ed719b92e098c07
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.13
|