Wagtail 教程 2:简单博客实现

这篇教程详细介绍了如何使用 Wagtail 和 Django 搭建一个简单的博客系统,包括创建博客列表和文章正文,设置文章按创建时间倒序排列,只显示已发布的文章,添加图像功能,以及实现标签功能。读者将学习到如何创建和管理模型、模板以及数据库迁移。
摘要由CSDN通过智能技术生成

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'),
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值