Tutorial Wagtail Version: 2.x
In Wagtail you can have two Richtext areas: a model field and a StreamField. In this article we'll cover the model field type.
RichText is a name for a WYSIWYG (what you see is what you get editor) editor in Wagtail. Because Wagtail is flexible and comes with the notion of Streamfields, we have two options to add RichText to our pages. The option you choose depends on the design of the website you're working with and the limitations it should apply. If you're wondering why we should limit users capabilities in a CMS, read The Zen of Wagtail.
The two options we have are:
In this tutorial we're only going to learn about the model field type. Although it's not a big jump to turning this into a SteramField, which is covered in this tutorial.
If you have experience with Django, you'll be familiar with the idea of Models. The short version is: Django models are a way to map data fields in your code to your database. Django and it's awesome ORM will take care of creating database tables, columns and indices for you if you tell it what you want.
For example, here's a basic Django model:
In the above code snippet, the models.CharField()
is the "Model Field" I'm referring to in this article. but CharFields and TextFields are boring. Sometimes you want to give the user an option to bold, italic, embed an image, create links and so on.. all without having to write any code.
In Wagtail, it's really easy to do this. Take a look below:
In the above code, we're doing the following:
models.TextField()
, it uses RichTextField()
instead.If you're following along and added the RichTextField, you'll want to run migrations at this point.
Once you're done that, open up your website and create a new Blog Post Page. It'll look like this:
The image above displays a new WYSIWYG editor with all the features pre-loaded. No more coding for your CMS administrators and content editors!
The last piece of this is adding the RichTextField value (the content that's written) to your template. This isn't a regular TextField like what Django provides. There's some beautiful magic behind the scenes for working with links, images, documents and embeds. For that, we need to use a Wagtail Template tag in our template.
Let's fill out some sample data when we create a new page.
I won't show you the entire base.html file, but I'll give you the blog_post_page.html template file. It's super simple, really, all we need to do is load one template tag and use it.
I'll explain the above, step-by-step just in case any part isn't clear.
richtext = RichTextField()
in our model file). And then we applied the |richtext
template tag to it. This will take the Wagtail editor (called Draftail) and make sense of the images, documents, embeds, links and anything else that's not using regular HTML markup, and turn it into regular HTML markup.If you've saved all of that, and you've applied any migrations you might have... preview or view the live page, and it'll look something like this:
If you're just looking for the source code, I've created a Gist for you to consume.
Setting Up RichText Streamfields
Posted on
In Wagtail you can have two Richtext areas: a model field and a StreamField. In this article we'll cover the Streamfield type.
Setting up Django Shell, Shell Plus and IPython
Posted on
Django comes with a python shell where you can import your models and test things out. It's not great out of the box, but we can make it better by using shell_plus (installation instructions are inside) and letting IPython take care of interactivity. This is all helpful for learning Wagtail.
How to Add a RichText StreamField to your Wagtail CMS Page
Posted on
In this video we are going to learn how to create a RichTextBlock StreamField, and then we're going to duplicate that StreamField and limit the number of features the editor gives us.
Extending The Draftail RichText Editor
Posted on
Wagtails RichText editor, Draftail, is very minimal out of the box. There are times when you need to extend it's functionality. Luckily for backend developers, we can extend the Draftail editor by writing a Wagtail Hook in Python. No JavaScript needed! We're create an inline <code> and a centered text feature in this tutorial.
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.