Hello.
What I want to implement now is the validation setting when saving after editing the page. I want the behavior when the title exceeds 33 characters to be the same as the behavior when the title is 0 characters.
In order to investigate the implementation method, this time I investigated the code of Wagtail. Previous article
Wagtail is open source, so if you search for "Wagtail Github" on the net, you will find Wagtail's Github page. I want to know which code is involved when saving the page, so let's search in the code for the time being.
A lot of django.po files are hit. This is a file for converting the words used in the page into another language. I don't think this file is deeply related to the contents of this time, so I will skip it.
wagtail / admin / views / pages.py looks suspicious. Look for the error message in this file as well to find out which function it is used in.
Apparently, the edit function on line 323 looks like that. The create and edit functions are similar. As you can guess from the name, the function that works when the page is created and when it is edited is different. (seems so)
This is where the error message is written.
messages.validation_error(request, _("The page could not be saved due to validation errors"), form)
If you look at the import statement at the beginning of the file, messages will
from wagtail.admin import messages, signals
You can see that it is wagtail / admin / messages.py. Various functions are written, but it seems that the processing shifts to the render function in general.
def render(message, buttons, detail=''):
return render_to_string('wagtailadmin/shared/messages.html', {
'message': message,
'buttons': buttons,
'detail': detail,
})
I found that I should use this ** messages ** to display the banner (?) Message that appears at the top of the page with wagtail.
Now, let's introduce the messages method. The method we've considered so far is to validate it on save with the save method in models.py. With this technique, you can set whether to save to the database. However, it seems difficult to deal with error messages. This time I would like to use hooks to get the error message.
hooks is a convenient function peculiar to wagtail, and it is possible to execute a program when a specific event occurs. Hooks also support before and after the Page object is edited, before and after the Page object is created, and so on. So, let's try using hooks after the Page object has been edited.
from django.http import HttpResponse
from wagtail.core import hooks
from wagtail.admin import messages
@hooks.register('after_edit_page')
def after_edit_page(request, page):
is_title_length_valid = True if (len(page.title) > 0 | len(page.title) < 33) else False
if not is_title_length_valid:
messages.error(request, "Title Count Error")
def index(request):
return HttpReesponse("Hello, world.")
The execution result looks like this.
http://localhost:8000/django-admin/wagtailcore/page/
You've implemented both database save validation and error messages.
It's been a year since I was doing it while watching the kid. ** -_- ** It seems that you can write articles in MarkDown with wagtail, so I think I'll try implementing it.
Recommended Posts