Skip to content

Instantly share code, notes, and snippets.

@KalobTaulien
Created July 21, 2018 19:27
Show Gist options
  • Save KalobTaulien/ca225bb28e791823eea1c6332e30821b to your computer and use it in GitHub Desktop.
Save KalobTaulien/ca225bb28e791823eea1c6332e30821b to your computer and use it in GitHub Desktop.
Wagtail 2: Adding Tags to Your Pages
<!-- Your Django Template -->
{% for tag in tags %}
<a href="{{ tag.slug }}">{{ tag }}</a>
{% endfor %}
"""Adding Tags to your Wagtail Page."""
from modelcluster.fields import ParentalKey
from modelcluster.tags import ClusterTaggableManager
from taggit.models import TaggedItemBase, Tag
from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel
from wagtail.core.models import Page
class StoryTag(TaggedItemBase):
"""Through Model for tags."""
content_object = ParentalKey("pages.StoryPage", related_name="tagged_items")
class StoryPage(Page):
# template = 'templates/story_page.html
tags = ClusterTaggableManager(
through=StoryTag,
blank=False,
help_text='Page tags. ie. "Science"',
)
# Other fields...
content_panels = [
# Other panels here
MultiFieldPanel([FieldPanel("tags")], heading="Tags"),
# More panels here
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment