Package: wagtail-schema.org
Add Schema.org JSON-LD to your websiteInstallingwagtail-schema.org supports Wagtail 2.0 upwards.Install using pip:$pipinstallwagtail-schema.orgAdd it to yourINSTALLED_APPSto use the Django template tags:INSTALLED_APPS=[...'wagtailschemaorg','wagtail.contrib.settings',]Usingwagtail-schema.orgsupports two types of schema entities:
site-wide entities and page-specific entities.
Site-wide entities might be the organisation that the site as a whole is about,
while page-specific entities might be a single person that the page in question is about.
Both sets of entities are optional, and
sites can implement only those that make sense.Site-wide entitiesA site-wide entity is printed on every page
using the{% ld_for_site %}template tag.
They should be entities that are relevant to the whole site,
such as the Organisation or Person that the site is about.
Multiple (or zero) site-wide entities can exist for a site.fromdjango.dbimportmodelsfromwagtail.contrib.settings.modelsimportregister_settingfromwagtailschemaorg.modelsimportBaseLDSettingfromwagtailschemaorg.registryimportregister_site_thingfromwagtailschemaorg.utilsimportextend@register_setting@register_site_thingclassTestOrganisation(BaseLDSetting):"""Details about this organisation"""name=models.CharField(max_length=100)phone_number=models.CharField(max_length=20)email=models.EmailField()twitter_handle=models.CharField(max_length=15)facebook_url=models.URLField()defld_entity(self):returnextend(super().ld_entity(),{'@type':'Organization','name':self.name,'email':self.email,'telephone':self.phone_number,'sameAs':[self.twitter_url,self.facebook_url,],})@propertydeftwitter_url(self):return'https://twitter.com/'+self.twitter_handleNoteEvery site-wide Thing should have a different@id.
By default, the@idis the Thing’surl.
You can change a Thing’s@idby overridingld_get_idorld_get_urlas required.Page-specific entitiesEach page can specify a list of relevant entities.
Use{% ld_for_object page %}to print these.# this code example assumes that the Wagtail version is 3.0 or higherfromdjango.dbimportmodelsfromwagtail.adminimportFieldPanelfromwagtail.modelsimportPagefromtestapp.modelsimportTestOrganisationfromwagtailschemaorg.modelsimportPageLDMixinfromwagtailschemaorg.utilsimportextend,image_ldclassPersonPage(PageLDMixin,Page):bio=models.TextField()date_of_birth=models.DateField()photo=models.ForeignKey('wagtailimages.Image',on_delete=models.PROTECT)content_panels=Page.content_panels+[FieldPanel('bio'),FieldPanel('date_of_birth'),FieldPanel('photo'),]defld_entity(self):site=self.get_site()returnextend(super().ld_entity(),{'@type':'Person','birthDate':self.date_of_birth.isoformat(),'image':image_ld(self.photo,base_url=site.root_url),'organisation':TestOrganisation.for_site(site),})In templateswagtail-schema.orgprovides two template tags:
one for printing out the site-wide entities and
one for page-specific entities.Django templatesMake sure thatwagtailschemaorgis in yourINSTALLED_APPS,
and add{% load wagtailschemaorg_tags %}to the top of your template.{% ld_for_site [site] %}Print all the site-wide entities for a site.
Takes an optionalsiteargument,
which defaults to the site from the current template request context.
Seeregister_site_thingfor more information on site-wide entities.{% ld_for_object [obj] %}Print all the entities forobj.objis optional, and defaults topagein the current template context.objshould implement theThingLDinterface.
Callsobj.ld_to_data_list, and prints all the entities returned.{% ld_print_entity entity %}Print an entity directly.entityshould be adictwith JSON-LD data.Jinja2 templatesAddwagtailschemaorg.jinja2tags.WagtailSchemaOrgExtensionto your Jinja2 extensions.{%ld.for_site([site])%}Print all the site-wide entities for a site.
Takes an optionalsiteargument,
which defaults to the site from the current template request context.
Seeregister_site_thingfor more information on site-wide entities.{%ld.for_object([obj])%}Print all the entities forobj.objis optional, and defaults topagein the current template context.objshould implement theThingLDinterface.
Callsobj.ld_to_data_list, and prints all the entities returned.{% ld.print_entity(entity) %}Print an entity directly.entityshould be adictwith JSON-LD data.