Wagtail 教程系列 记录了基于 Wagtail 搭建博客站点的整个过程,博客站点 所呈现的即是搭建过程的最新效果。
更多 Wagtail 内容:https://slowread.cn/wagtail-tutorials
博客列表和文章正文
- 执行 python manage.py startapp blog 创建 blog app.
- 编辑 slowread/settings/base.py 在 INSTALLED_APPS 中添加 blog app.
博客列表实现
编辑 blog/models.py 添加以下内容:
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full")
]
Copy
执行 python manage.py makemigrations && python manage.py migrate
因为模型名字为BlogIndexPage,因此模板文件的名字就成了blog/templates/blog/blog_index_page.html,模板文件内容如下:
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block body_class %}template-blogindexpage{% endblock %}
{% block content %}
<h1>{
{ page.title }}</h1>
<div class="intro">{
{ page.intro|richtext }}</div>
{% for post in page.get_children %}
<h2><a href="{% pageurl post %}">{
{ post.title }}</a></h2>
{
{ post.specific.intro }}
{
{ post.specific.body|richtext }}
{% endfor %}
{% endblock %}
Copy
博客正文实现
编辑 blog/models.py 添加以下内容:
from django.db import models
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.search import index
# Keep the definition of BlogIndexPage, and add:
class BlogPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
FieldPanel('date'),
FieldPanel('intro'),