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.
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 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 ①
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. ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
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.
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.
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