Hi! I’m Patryk. I’m a Python Backend developer at Profil Software. In my work, I’ve had a chance to use Wagtail to build a few applications. It’s a free and open-source content management system written in Python that is based on Django. That makes it easy and speeds up development. In this article, I would like to share a few tips with you that came out of my work for Python Development Company. I hope that it will help you build Wagtail applications more efficiently.
Before creating any application, you should consider what your website would look like when it comes to the hierarchy of pages. Wagtail offers support for organizing your pages into folders and thus creating paths to them. You should avoid flat structure as it complicates using the admin panel because over time there can be a lot of pages and navigating between them can become problematic. Also, a well-designed page structure helps you to configure SEO for your pages.
You can force the user to follow your hierarchy by using
subpage_types properties in your page model. For a simple blog, I would create a hierarchy like this:
We have a root. Each category has its page and generates the proper path. Each article belongs to a category.
Here is the code:
For images consider using `wagtail.images.models.imageField`
First of all, it has a wagtail panel that looks user-friendly. Another important feature is that you can add custom fields, for example, caption. You can find out here how you can do it. And last but not least, the model allows you to easily convert and scale images by using
get_rendition method. For example,
image.get_rendition(“fill-800x600|format-webp|webpquality-95”) produces a webp image with size 800x600 and quality 95 and saves it when trying to use it again.
And here is how it looks like
When I was creating a blog application I had to create a post model with an extended content field. As you know, the field can contain anything, starting from GitHub gist, special styled links, and ending with a gallery. Unfortunately, the Wagtail rich text editor wasn’t sufficient. I found that there is a StreamField. After a few hours of studying, I managed to create a very nice-looking widget that the client could use and add all these fancy things. You just simply create “blocks” with desired fields and then include them in the StreamField. With this field, you can create very complex widgets. More about it here.
And the PostPage
And the AdBlock
I created a home page. Initially, there were only a few things with content. Over time, It was growing and growing, and adding new content became problematic because finding a proper place to put new content took years. I had to scroll over all these inputs and search for the one I am looking for. I found that wagtail shares a feature that allows you to divide a page into tabs! After moving stuff around a little, it looked much better and more intuitive for users. Learn here how to do it.
Example code for dividing blog into tabs:
Over the years, the Wagtail community was growing and thus many libraries were made that are extending the wagtail features. Founding the proper one can be challenging, and you don’t know what is available. I found the Github page where you can find most of them. Moreover, there are presentations, videos, podcasts, and many more materials.
That’s all the tips I wanted to share with you. I hope that you can use some of them to build better wagtail applications.
For other articles on Python you may refer to our software development blog from which you'll learn what is Django Rest Framework and how to effectively use it, why are Python metaclasses so special and more.