[PYTHON] I touched Wagtail (1) and let's override the save method.

Introduction

Hello

I was developing with Wagtail a while ago. There are not many Japanese documents in Wagtail, so you are required to read English documents.

As an engineer, the ability to read English literature is very important, ** I wish there was a document written in Japanese ** I think many people think that. ~~ I think ~~

So, I would like to write the code using Wagtail and output it. This time is the first of them.


About Wagtail

I think some people think of Wagtail in the first place.

Wagtail is a Django-based CMS OSS /word15874.html). WordPress runs on PHP, while Wagtail runs on Python. As you can see from the Release Notes, the development is quite active.

By the way, Wagtail is a word that corresponds to Sekirei in Japanese. The Wagtail logo is also made with a Sekirei motif.


Let's prepare the environment

Let's touch it immediately. I'm on a mac. Regarding the environment construction, I referred to the following site.

reference: Wagtail Recommendations (1) A story about choosing Wagtail by comparing and examining Django-based CMS Make a simple blog system with wagtail ~ Learn Wagtail ①


Let's write the text

Access the CMS from localhost and click the "Page Button". Then click "ADD CHILD PAGE" at the top of the screen. You can edit your blog here.

If you haven't edited any code, you can only enter the title. Refer to the reference site below and set so that you can write the text.

Reference: Customize Wagtail page

home/models.py



  1 from django.db import models
  2 
  3 from wagtail.core.models import Page
  4 from wagtail.core.fields import RichTextField
  5 from wagtail.admin.edit_handlers import FieldPanel
  6 
  7 
  8 class HomePage(Page):
  9     """
10 Page model
 11     """
 12     body = RichTextField(blank=True)
 13     content_panels = Page.content_panels + [FieldPanel("body", classname="full"),]

Let's take a look at the code. The code for this model is Page Inherit .jp / python-class-2) HomePage model is written. Added the body as RichTextField, that is, the model of the text. Don't forget to add FieldPanel. ~~ There is a person who has forgotten here ~~

Let's also migrate.


python manage.py makemigrations
python manage.py migrate

I think the display screen looks like this. ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ スクリーンショット 2019-12-23 15.24.51.png


Let's write the operation when saving the title and text

After filling in the title and body, you can save the content by clicking the tabs at the bottom of the screen. As a process at that time, I will try to validate the title and body. This time I will limit the number of characters. ** Save the title and body for testing **

home/models.py


  1 from django.db import models
  2 
  3 from wagtail.core.models import Page
  4 from wagtail.core.fields import RichTextField
  5 from wagtail.admin.edit_handlers import FieldPanel
  6 
  7 
  8 class HomePage(Page):
  9     """
10 Page model
 11     """
 12     body = RichTextField(blank=True)
 13     content_panels = Page.content_panels + [FieldPanel("body", classname="fu    ll"),]
 14 
 15     def save(self, *args, **kwargs):
 16         """save function"""
 17         print("Entered save function")
 18         breakpoint()

Overriding the save method in django.

breakpoint()

This is a function that calls the debugger PDB that can be used with Python. Let's run it right away.

  1. Start a local server
  2. Write breakpoint () anywhere
  3. Save the edited file
  4. Execute any command The following log is now output.
Entered save function
--Return--
> /Users/Username/mysite/home/models.py(18)save()->None
-> breakpoint()
(Pdb) HomePage
<class 'home.models.HomePage'>
(Pdb) HomePage.objects
<django.db.models.manager.BasePageManagerFromPageQuerySet object at 0x10ebee410>
(Pdb) HomePage.objects.all()
<PageQuerySet [<HomePage: Home>, <HomePage: title>]>
(Pdb) instance = HomePage.objects.all()
(Pdb) instance
<PageQuerySet [<HomePage: Home>, <HomePage: title>]>
(Pdb) instance[1]
<HomePage: title>
(Pdb) instance[1].title
'title'
(Pdb) len(instance[1].title)
5

You can find out the length of the string by using the len function.

When developing with Django or Wagtail, I start the debugger like this and check the contents of the object. After that, if you write a conditional expression, it seems that you can validate the number of characters in the title.

According to this site "What is the effective number of characters in a title with Google SEO", the title character can be up to about 32 characters. Sounds just right. This time I would like to process it so that it does not exceed 33 characters.

home/models.py


 16     def save(self, *args, **kwargs):
 17         """save function"""
 18         print("Entered save function")
 19         breakpoint()
 20         instance = HomePage.objects.all() #I forgot to erase
 21         titleCharCount = len(self.title)
 22         if ((titleCharCount < 0) | (titleCharCount > 32)):
 23             #Not validated
 24             return
 25         else:
 26             #Validation passed
 27             return super(HomePage, self).save()

The object is not saved on line 24. It is saved on line 27. Let's check it using a debugger. If you write a similar process, the text will be validated.


at the end

You haven't implemented the error message that appears at the top of the screen when the object cannot be saved. Next time I would like to write the output of it.

Recommended Posts

I touched Wagtail (1) and let's override the save method.
I touched Wagtail (3). Investigation and implementation of pop-up messages.
Override save method in django-models
I touched the Qiita API
I touched Wagtail (2). Introducing django-extensions.
I touched Tensorflow and keras
I'll inherit and override the class
I touched the data preparation tool Paxata
Let's play with Python Receive and save / display the text of the input form
12. Save the first column in col1.txt and the second column in col2.txt
I tried to save the data with discord
I tried the least squares method in Python
I touched some of the new features of Python 3.8 ①
Wagtail Recommendation (4) Let's pass the context to the template
I read and implemented the Variants of UKR
[Memorandum] ① Get and save tweets ~ I want to identify the news tweets that are spread ~