Wagtail CMS: How to customize default Wagtail Page property values

This video is from Wagtail CMS 2023 .

Wagtail CMS: How to customize default Wagtail Page property values
0:10:44
Published November 29, 2023
3,340 views

Tutorial: https://learnwagtail.com/tutorials/customizing-default-property-values/

Git Commit: https://github.com/CodingForEverybody/learn-wagtail/commit/234080b19f7072b2f7434247f3aa9c95766a81bc

Learn Wagtail from scratch with the official Wagtail for Beginners Course
https://learnwagtail.com/wagtail-for-beginners/

Used in this video: Wagtail 2.4, Python 3.7, Django 2.1.5

Outro Template made by Grabster - Youtube.com/GrabsterTV

#Wagtail #Django #Python

Summary

Wagtail’s built-in Page fields can be customised from a page model even when they are inherited and not declared there. By using `HomePage._meta.get_field(...)`, you can change a field’s `verbose_name`, `help_text`, and `default`—for example, for the title and slug—and set help text to `None` to remove it. The same technique works for custom Django model fields and inherited fields, although the speaker notes it is an occasional rather than everyday requirement.

Key takeaways

  • Access inherited Wagtail fields with `HomePage._meta.get_field('field_name')`.
  • Set `verbose_name` to change how a field’s label appears in the Wagtail admin.
  • Set `help_text` to replace the standard guidance, or to `None` to remove it.
  • Set `default` to prepopulate fields such as a page title or slug when creating a page.
  • The approach also applies to custom fields and fields inherited from other model classes.

Summarised automatically from the transcript.

Transcript

2,045 words · auto-generated Show

Automatically transcribed, so expect mistakes in names and technical terms.

0:00

Hello and welcome back to another lesson on learning Wagtail. In this video, we're going to learn how to change some of the default values, such as the help text, the title, and a few other properties in a default Wagtail page in the CMS admin. Now if that confused you, let's just dive right into this and I'll show you exactly what I mean. Now the first thing I need to do is get inside of my environment. So I need to CD websites and I need to go into my site And then I'm going to type pipenv shell, but you might be using uh Docker or something else. You just need to get inside of there. And then manage. Dot pie run server now I know I already have port 8000 allocated so I'm going to use port 8000 and one

0:47

All looks good there. So let's open up Firefox and let's go to localhost port 8000N1 slash admin. So we can see that Wagtail 2. 5 is out. I have not upgraded just yet, but I will make an upgrade video on that. But we want what we want to edit here is let's uh let's just select the home page and Let's say we want a default homepage title. We have some banner settings, promotional stuff, settings stuff in here. So these are custom tabs. So if you're not quite familiar with how to create these, in the previous video, we created some custom tabs and renamed the existing ones. In here this was just called uh promotional I believe and we could also apply a default slug and when you hover over a field you can see that there is

1:35

maybe I can make this Tad bigger. When you hover over a field, you can see that there's help text in here. And because this comes from a default Wagtail page, it's Not exactly easy to change it. We can't just tell it to have different help text inside of our model. So inside of this video, we are going to change the default slug, the default page title, and Maybe let's change the help text and maybe let's see what happens if we get rid of the help text. Alright, so at this point I'm just going to open up my editor. I'm using VS Code, but you might be using Sublime or Atom. Maybe even PyCharm. It doesn't really matter what your editor is, you just need to open up some sort of text editor. And we're going to open up home slash models. py. And in this file, we're simply going to change some of the values outside of the model.

2:24

So let's dive right into this. So I'm going to open up home Models. py and here we can see we have all sorts of stuff in here. We've got an orderable, we have a home page. All this good stuff. But where we wanted to change our main page title, for instance, we wanted to change this default title, or maybe we wanted to change this help text. We'll change both in this video. It's going to be a little bit tricky to change this when title already exists, but we can't really access it. Now in VS Code and in a lot of other editors, there is this nice functionality where you can jump. Now if this is set up on your machine properly, you can just hit F12, I believe it is, or on Mac

3:10

for me, it's function F12, and it will jump straight to the Wagtail page itself, and we can see that we've got a title here And this is where we're getting our title. It has a verbose name, max length, and help text. Well, let's go ahead and change that verbose name right away. And this is actually going to be a very interesting little change here. So uh that actually that actually closed down my home models. py. And we don't have that title. So we don't have Title in here, that just does not exist. That gets inherited from page. So what we need to do is we need to come down to the very bottom here Get rid of all that. There's too many new lines in there. And uh let's just create something outside of this class. Underscore, nope, not yet.

3:56

I got ahead of myself there. Meta get field and we're going to call this function and we're just going to get the title field. And then we're going to set the verbose name. So let's change the verbose name, which you can actually see is set right here on the class itself, but in the title it is set right here. So we've got two verbose names. So we're grabbing the homepage, we're getting some metadata, we're getting the field called title, and we're going to change the verbose name And we're just going to change that to any verbose name. Just as an example here. So let's go ahead and create a new line Terminal end looks nice and happy, that's always good news. And let's refresh our page and we're going to see.

4:43

Actually, let's stay on this page. And we're going to see that right in here where it says title, that's going to change. So yes, now we want to leave. And it changed from title to any verbose name. Now you can do this on any field in a model. It doesn't just have to be a Wagtail field that they give you by default. It can be a field that you've already added that you might want to modify a little bit later. And again, it doesn't need to be just that verbose name, but it can also be the default value or the default title. Really anything that comes with a Django model. You can change that. So let's take a look at what I mean. In the Django model, we have our title is equal to models. charfield or Carfield. And we've got the verbose name, and that's what we changed.

5:29

Next we want to change our help text, and it's the exact same concept, but we should also know that with a char field comes a default value. And we can change that as well. So I'm just going to copy this line over and change the last one here. We're not going to change the verbose name. We're going to change the help text. And let's change this to custom help text. And again, that default help text is the page title as you'd like it to be seen by the public. So let's give this page a refresh and let's see that it changes to custom help text. So now we have a custom verbose name and a custom help text. Now it's really, really easy to get rid of this.

6:14

All we have to do is set this To none. When we set this to none, our help text is going to go away completely. And just waiting for it to refresh here. And when I hover over, we don't see that there's any help text there anymore. So that is a way of getting rid of it. If you ever wanted to get rid of it, you can also customize it. Now in my opinion and in my experience, it's not too often you need to customize either of those. Now there's a question that came up not too long ago about how we set a default value. Now this already has a value in here But let's go ahead and create a brand new home page. So let's click on home and we want to go find our route. Let's scroll up. Grab our root page, yes, leave the page, we did not do anything.

7:00

And let's add a child page So our child page is going to be a home page by default. And again it says to any verbose name, and that's what we changed the title of the page to. But we want to put a default value in here, and maybe we want to put a default value in the slug as well. So to change those values, all we have to do is basically copy this line over and simply target a different field. Now we want to target the title field again. We're going to change that for a slug, and we just want to change the default value, which is default. And in HR field, that's not really any different than saying the default in here is going to be something. Now this is coming from a package called Wagtail and we don't get to edit that, but what we can do

7:47

is we can specify by using meta get field, grab the title, grab the default, and what is the default going to be? Let's say some default title for homepage. Terminal end looks good. And let's go ahead and go to our content. We see that there is no default title in there Let's see that there is a default title. Now with this one we're going to run into a particular problem. We've added a default title, but if we were to save this page, well there's no carousel images, so that's adding a different type of default. That one actually we're not going to cover today, but the promotional stuff, we see that slug is also mandatory, but not filled out. So maybe we want to add a slug in there as well. So let's open up our editor

8:32

Make some room down here. And let's not target the title. Let's target the slug and change the slug to something slug-like. So, some default title for home page. Something like that. Refresh our page, and let's go over to promotional stuff again, and we see our slug is in there So that's how we change default values on a field that we don't actually really have too much access to. Now this is a feature that will occasionally come up, but it's not super popular. But what's really nice about this is you can customize this at any point in time on any model. And it doesn't just have to be a Wagtail field, it can be your own field down the road as well. Or if you create a particular class and then you inherit from that class, you can also change the values of that field as well.

9:24

So this is actually a really, really handy, handy feature. Now again, this isn't the most popular thing that you're going to have to do, but I have seen this come up several times and has actually come up in my career as a Wagtail developer. And so I just thought this would be an interesting thing to share with you. Now I'm going to leave this code in here, but I'm going to comment it out so it looks back to normal, but if you were to clone this repo, all you'd have to do is uncomment the code to see the changes. And as always, because this video has code, I'm going to leave the description to the git commit in the YouTube description down below And if you have any suggestions for another video, don't forget you can leave them in the comment section down below. I look at every single comment that comes in, so feel free to give your suggestions in there. Hey there, my name is Caleb Tallinn.

10:10

I am a Wagtail Core team member, and I just wanted to say thank you for watching this video about Learning Wagtail CMS. If you enjoyed this video, there are plenty more where this came from, all available on learnwagtail. com And if you would like to dive into any particular subject, you can always go to docs. wagtail. io. The documentation is quite fantastic and in true Python fashion, it is very well written. And hey, if you like this video, don't forget you can always share, subscribe, or leave a comment down below. I love hearing from the community, and I look forward to seeing you in the next video.

Questions this talk answers

How do I change the label of a built-in Wagtail Page field?

Use the model’s metadata API to retrieve the inherited field, such as `title`, and update its `verbose_name`. This works for built-in Wagtail fields as well as fields defined on your own models.

Discussed at 3:56

How do I customize or remove the help text for a Wagtail Page field?

Retrieve the field with `Meta.get_field()` and assign a new value to its `help_text`. Setting `help_text` to `None` removes the help text entirely.

Discussed at 5:29

How do I set default values for inherited Wagtail Page fields like title and slug?

Retrieve the relevant field through the model metadata and change its `default` value—for example, setting a default title or a default slug for newly created pages.

Discussed at 7:00

Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.

More videos from Wagtail CMS