Tutorial Wagtail Version: 2.x
In this tutorial we'll discover how to add Tags (with an autocomplete feature) to Wagtail Pages. We'll also learn how to filter blog posts by a certain tag.
Tags and categories are very similar, but ultimately they serve a different purpose. We've already covered how to add categories, and in this tutorial we're learning how to add tags to Wagtail CMS pages. The nice thing about Tags is they come with an autocomplete feature. Because tags can be easily forgotten, Wagtail helps us re-use tags by giving an autocomplete feature (suggestions based on what you're currently typing).
Tags are an interesting subject in Wagtail. We use a package called Taggit which does a lot of work behind the scenes and makes implementing tags very easy. Before we can add tags to a page, we need to add a new Django Model.
Note: We're adding tags to a Blog Detail Page, but you can adjust this to work on any page.
Next we need to add the tags
field to our BlogDetailPage
.
Lastly we need to add tag links to our Blog Detail Page. You can copy and paste this code into your blog_detail_page.html (or whichever template you're applying tags to)
This part is bonus, but if you wanted to click one of the tags in the code snippet above you'll want your Blog Listing Page (A.K.A. Blog Index Page) to filter your posts. In the course we've added pagination, so make sure you read the entire file for pagination. Below is just a simple filter.
The above code will check for ?tag=your-tag-slug-here
in your URL and filter your tags based on the slug of the tag. So when you click a tag link on a detail page, it will bring you to a Blog Listing Page that only shows blog posts with that particular tag.
As always, the code is completely open source. Here's the direct link to the GitHub commit: https://github.com/CodingForEverybody/learn-wagtail/commit/8d4240096482baa207ca7bef31bf5f8aed263f19
How to Subclass Wagtail Pages
Posted on
Subclassing is having a class (in this case it's a Wagtail Page) that can be used for other classes (Wagtail Page's). The parent class has all the common attributes for the child pages, and every child page will inherit everything from it's parent. In this lesson we'll explore that by creating a subclassed Article and Video Blog Page that share a common parent, and then we'll extend the functionality of both subclassed pages by adding new fields.
How to Paginate Your Wagtail Pages
Posted on
Pagination is the ability to click through "pages". You most commonly see this on a Blog Listing Page, where you have "page 1 of 4" for example. In this lesson we're going to use Django Paginator right out of the box to add pagination to our Wagtail Blog Listing Page. No 3rd party packages, no craziness, and minimal maintenance. Just beautiful Wagtail and Django working together in 11 lines of code in our Wagtail Page Pagination.
Routable Pages
Posted on
Routable Pages allow us to create "subpages" under any regular Wagtail Page. Essentially, we can create pages with urls that aren't accessible through the Wagtail CMS admin. We'll learn how to implement routable pages, how to add additional context to the new page, how to render a new page template, how to reverse the routable page url in the template and how to reverse the routable page url in the Wagtail model.
Adding Recaptcha to Your Contact Forms
Posted on
Contact forms are notorious for collecting spam from bots. In this tutorial we'll install a package to automatically add Google Recaptcha to our contact forms. We'll explore how to get the API keys, as well.
This course covers everything from basic installation to advanced features like custom blocks and API integration, it's perfect for developers looking to enhance their skills with this powerful CMS.